-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtgbot.go
165 lines (141 loc) · 3.73 KB
/
tgbot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package application
import (
"fmt"
"log"
"os"
"strings"
"sync"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
tg_extra "github.com/xsymphony/telegram-gemini-bot/pkg/tg-extra"
)
type TgBot struct {
client *tgbotapi.BotAPI
}
func newTagBot() func() *TgBot {
var (
once sync.Once
bot *TgBot
)
return func() *TgBot {
once.Do(func() {
token := os.Getenv("TGBOT_TOKEN")
client, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Fatal(err)
}
client.Debug = true
log.Printf("Authorized on account %s\n", client.Self.UserName)
bot = &TgBot{
client: client,
}
})
return bot
}
}
var tgBot = newTagBot()
func (bot *TgBot) StartWebhook() error {
domain := os.Getenv("DOMAIN")
link := domain + "/message"
wh, _ := tgbotapi.NewWebhook(link)
_, err := bot.client.Request(wh)
return err
}
func (bot *TgBot) StopWebhook() error {
_, err := bot.client.Request(&tgbotapi.DeleteWebhookConfig{
DropPendingUpdates: true,
})
return err
}
func (bot *TgBot) RecvMessage(update *tgbotapi.Update) error {
if update.Message == nil {
return nil
}
if err := bot.handleCommand(update); err != nil {
log.Println(err)
return err
}
isUseful := bot.filterUselessGroupMessage(update)
if !isUseful {
return nil
}
if err := bot.handleText(update); err != nil {
log.Println(err)
return err
}
return nil
}
func (bot *TgBot) handleCommand(update *tgbotapi.Update) error {
if !update.Message.IsCommand() {
return nil
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
switch update.Message.Command() {
case "clear":
msg.Text = "会话记录已清除,开启一场新的对话吧。"
sessions.delete(update.Message.From.ID)
default:
msg.Text = "不支持的命令,目前支持:/clear"
}
if _, err := bot.client.Send(msg); err != nil {
return err
}
return nil
}
func (bot *TgBot) handleText(update *tgbotapi.Update) error {
if update.Message.IsCommand() {
return nil
}
if update.Message == nil || update.Message.Text == "" {
return nil
}
log.Printf("[telegram]%s: %s\n", update.Message.From.UserName, update.Message.Text)
_ = bot.typing(update.Message.Chat.ID)
reply, err := sessions.Ask(update.Message.From.ID, update.Message.Text)
if err != nil {
return bot.reply(update.Message.MessageID, update.Message.Chat.ID, fmt.Sprintf("gemini响应错误:%s", err.Error()))
}
return bot.reply(update.Message.MessageID, update.Message.Chat.ID, reply)
}
func (bot *TgBot) typing(chatID int64) error {
_, err := bot.client.Send(tgbotapi.NewChatAction(chatID, "typing"))
return err
}
func (bot *TgBot) reply(replyMessageID int, chatID int64, content string) error {
content = tg_extra.EscapeMarkdownV2(content, 0)
msg := tgbotapi.NewMessage(chatID, content)
msg.ReplyToMessageID = replyMessageID
msg.ParseMode = "MarkdownV2"
_, err := bot.client.Send(msg)
log.Printf("[telegram]%s: %s\n", bot.client.Self.UserName, content)
return err
}
func (bot *TgBot) isAtMe(update *tgbotapi.Update) bool {
for _, entity := range update.Message.Entities {
if entity.Type == "mention" {
if strings.Contains(update.Message.Text, bot.client.Self.UserName) {
return true
}
}
}
return false
}
func (bot *TgBot) isReplyMe(update *tgbotapi.Update) bool {
if update.Message != nil && update.Message.ReplyToMessage != nil && update.Message.ReplyToMessage.From.UserName == bot.client.Self.UserName {
return true
}
return false
}
func (bot *TgBot) filterUselessGroupMessage(update *tgbotapi.Update) bool {
// 私聊消息不做特殊处理
if update.FromChat().IsPrivate() {
return true
}
if bot.isAtMe(update) {
update.Message.Text = strings.ReplaceAll(update.Message.Text, "@"+bot.client.Self.UserName, "")
return true
}
if bot.isReplyMe(update) {
return true
}
return false
}