-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait.go
More file actions
104 lines (89 loc) · 2.92 KB
/
wait.go
File metadata and controls
104 lines (89 loc) · 2.92 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
package conversation
import (
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)
type ContextBool func(ctx *ext.Context) bool
// Waits for the next update in the chat of the provided context.
// Note that the context must have the `EffectiveChat` set in order to work.
// The validate parameter tells if the received update is valid or not,
// it can be `nil`. If the validate function is set and it returned `false`,
// the received update will not be counted and will be waiting for the next.
func (c *Conversation) Wait(ctx *ext.Context, validate ContextBool) *ext.Context {
c.mu.Lock()
_, ok := c.channels[ctx.EffectiveChat.Id]
if !ok {
c.channels[ctx.EffectiveChat.Id] = make(chan *ext.Context)
}
channel := c.channels[ctx.EffectiveChat.Id]
c.mu.Unlock()
answer := <-channel
if validate != nil && !validate(answer) {
return c.Wait(ctx, validate)
}
return answer
}
// Cancels the update listener for the chat of the provided context.
// Returns bool telling if it was canceled or not.
// It cancels it by sending `nil` to the channel.
func (c *Conversation) Cancel(ctx *ext.Context) bool {
c.mu.Lock()
channel, ok := c.channels[ctx.EffectiveChat.Id]
c.mu.Unlock()
if ok {
channel <- nil
delete(c.channels, ctx.EffectiveChat.Id)
return true
}
return false
}
// Waits for a next message to be received in the chat of the provided context.
func (c *Conversation) WaitForMessage(ctx *ext.Context, validate ContextBool) *ext.Context {
return c.Wait(
ctx,
func(ctx *ext.Context) bool {
validateResult := true
if validate != nil {
validateResult = validate(ctx)
}
return ctx.Message != nil && validateResult
},
)
}
// Waits for a message to be received or edited in the chat of the provided context.
func (c *Conversation) WaitForEffectiveMessage(ctx *ext.Context, validate ContextBool) *ext.Context {
return c.Wait(
ctx,
func(ctx *ext.Context) bool {
validateResult := true
if validate != nil {
validateResult = validate(ctx)
}
return ctx.EffectiveMessage != nil && validateResult
},
)
}
// Waits for a message to be edited in the chat of the provided context.
func (c *Conversation) WaitForEditedMessage(ctx *ext.Context, validate ContextBool) *ext.Context {
return c.Wait(
ctx,
func(ctx *ext.Context) bool {
validateResult := true
if validate != nil {
validateResult = validate(ctx)
}
return ctx.EditedMessage != nil && validateResult
},
)
}
// Waits for a callback query to be received in the chat of the provided context.
// `message` is the message that we are waiting for the callback of its buttons,
// it should be provided so there are no conflicts with older messages with buttons.
func (c *Conversation) WaitForCallback(ctx *ext.Context, message *gotgbot.Message) *ext.Context {
return c.Wait(
ctx,
func(ctx *ext.Context) bool {
return ctx.CallbackQuery != nil && ctx.CallbackQuery.Message.MessageId == message.MessageId
},
)
}