/
send_answer.go
76 lines (65 loc) · 1.77 KB
/
send_answer.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
package logic_handlers
import (
"io"
"log"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
"github.com/pkg/errors"
)
var (
defaultSendMessageOpts = &gotgbot.SendMessageOpts{
ParseMode: "html",
}
)
func replyWithStatesInConversation(
b *gotgbot.Bot, ctx *ext.Context,
msg string, success bool,
failureStateName, successStateName string,
opts *gotgbot.SendMessageOpts,
) (nextState error) {
// If not success -> try again
if !success {
return replyInConversation(b, ctx, msg, failureStateName, opts)
}
// If success -> go to next state
return replyInConversation(b, ctx, msg, successStateName, opts)
}
func replyInConversation(
b *gotgbot.Bot, ctx *ext.Context,
msg string, nextStateName string,
opts *gotgbot.SendMessageOpts,
) (nextState error) {
sendMessage(b, ctx, msg, opts)
return handlers.NextConversationState(nextStateName)
}
func endConversation(b *gotgbot.Bot, ctx *ext.Context, msg string) (nextState error) {
sendMessage(b, ctx, msg, nil)
return handlers.EndConversation()
}
func sendMessage(b *gotgbot.Bot, ctx *ext.Context, msg string, opts *gotgbot.SendMessageOpts) {
sendOpts := defaultSendMessageOpts
if opts != nil {
sendOpts = opts
}
_, err := b.SendMessage(ctx.EffectiveChat.Id, msg, sendOpts)
if err != nil {
log.Printf("b.SendMessage: %v", err)
}
}
func sendDocument(
b *gotgbot.Bot, ctx *ext.Context,
file io.Reader, fileName, caption string,
) error {
_, err := b.SendDocument(ctx.EffectiveChat.Id, gotgbot.NamedFile{
File: file,
FileName: fileName,
}, &gotgbot.SendDocumentOpts{
Caption: caption,
ReplyToMessageId: ctx.EffectiveMessage.MessageId,
})
if err != nil {
return errors.Wrap(err, "b.SendDocument")
}
return nil
}