-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
92 lines (79 loc) · 1.92 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package tele_prompt
import (
"context"
"errors"
"log"
"sync"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var (
ErrBadResponse = errors.New("response is incompatible with the prompt")
ErrOutofRange = errors.New("response is out of range")
ErrValidationFailed = errors.New("validation failed")
ErrMaxRetries = errors.New("max retries has been reached")
)
type Config struct {
DefaultTimeout time.Duration
}
func DefaultConfig() Config {
return Config{
DefaultTimeout: time.Minute * 5,
}
}
type Manager struct {
config Config
channels sync.Map
}
func NewManager(config Config) *Manager {
return &Manager{
config: config,
channels: sync.Map{},
}
}
// Handle receives a new update and returns true if app expected user to answer to something
// Otherwise, returns false
func (m *Manager) Handle(update tgbotapi.Update) bool {
userId := userIdFromUpdate(update)
if userId == -1 {
return false
}
channelInterface, has := m.channels.Load(userId)
if !has {
return false
}
select {
case channelInterface.(chan tgbotapi.Update) <- update:
return true
default:
log.Printf("teleprompt: channel was full, unexpected error\n")
return false
}
}
// Any does not care about types and different prompts, it receives the entire update
func (m *Manager) Any(ctx context.Context, userId int64) (tgbotapi.Update, error) {
ctx, cancel := context.WithTimeout(ctx, m.config.DefaultTimeout)
defer cancel()
ch := make(chan tgbotapi.Update, 1)
m.channels.Store(userId, ch)
// delete the current channel
defer func() {
m.channels.Delete(userId)
}()
select {
case <-ctx.Done():
return tgbotapi.Update{}, ctx.Err()
case update := <-ch:
close(ch)
return update, nil
}
}
func userIdFromUpdate(update tgbotapi.Update) int64 {
if update.Message != nil {
return update.Message.From.ID
}
if update.CallbackQuery != nil {
return update.CallbackQuery.From.ID
}
return -1
}