/
bot.go
113 lines (96 loc) · 2.87 KB
/
bot.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package tgbot
import (
"log"
"os"
"time"
"github.com/d0anisimov/BinTangBot/src/chatgpt"
"github.com/d0anisimov/BinTangBot/src/markdown"
"github.com/d0anisimov/BinTangBot/src/ratelimit"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Bot struct {
Username string
api *tgbotapi.BotAPI
editInterval time.Duration
}
func New(token string, editInterval time.Duration) (*Bot, error) {
var api *tgbotapi.BotAPI
var err error
apiEndpoint, exist := os.LookupEnv("TELEGRAM_API_ENDPOINT")
if exist && apiEndpoint != "" {
api, err = tgbotapi.NewBotAPIWithAPIEndpoint(token, apiEndpoint)
} else {
api, err = tgbotapi.NewBotAPI(token)
}
if err != nil {
return nil, err
}
return &Bot{
Username: api.Self.UserName,
api: api,
editInterval: editInterval,
}, nil
}
func (b *Bot) GetUpdatesChan() tgbotapi.UpdatesChannel {
cfg := tgbotapi.NewUpdate(0)
cfg.Timeout = 30
return b.api.GetUpdatesChan(cfg)
}
func (b *Bot) Stop() {
b.api.StopReceivingUpdates()
}
func (b *Bot) Send(chatID int64, replyTo int, text string) (tgbotapi.Message, error) {
text = markdown.EnsureFormatting(text)
msg := tgbotapi.NewMessage(chatID, text)
msg.ReplyToMessageID = replyTo
return b.api.Send(msg)
}
func (b *Bot) SendEdit(chatID int64, messageID int, text string) error {
text = markdown.EnsureFormatting(text)
msg := tgbotapi.NewEditMessageText(chatID, messageID, text)
msg.ParseMode = "Markdown"
if _, err := b.api.Send(msg); err != nil {
if err.Error() == "Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message" {
return nil
}
return err
}
return nil
}
func (b *Bot) SendTyping(chatID int64) {
if _, err := b.api.Request(tgbotapi.NewChatAction(chatID, "typing")); err != nil {
log.Printf("Couldn't send typing action: %v", err)
}
}
func (b *Bot) SendAsLiveOutput(chatID int64, replyTo int, feed chan chatgpt.ChatResponse) {
debouncedType := ratelimit.Debounce(10*time.Second, func() { b.SendTyping(chatID) })
debouncedEdit := ratelimit.DebounceWithArgs(b.editInterval, func(text interface{}, messageId interface{}) {
if err := b.SendEdit(chatID, messageId.(int), text.(string)); err != nil {
log.Printf("Couldn't edit message: %v", err)
}
})
var message tgbotapi.Message
var lastResp string
pollResponse:
for {
debouncedType()
select {
case response, ok := <-feed:
if !ok {
break pollResponse
}
lastResp = response.Message
if message.MessageID == 0 {
var err error
if message, err = b.Send(chatID, replyTo, lastResp); err != nil {
log.Fatalf("Couldn't send message: %v", err)
}
} else {
debouncedEdit(lastResp, message.MessageID)
}
}
}
if err := b.SendEdit(chatID, message.MessageID, lastResp); err != nil {
log.Printf("Couldn't perform final edit on message: %v", err)
}
}