-
Notifications
You must be signed in to change notification settings - Fork 0
/
telega32.go
231 lines (206 loc) · 5.43 KB
/
telega32.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
zhub4 - Система домашней автоматизации на Go
Copyright (c) 2023 GSB, Georgii Batanov gbatanov @ yandex.ru
*/
package telega32
import (
"errors"
"fmt"
"log"
"os"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func init() {
}
// Сообщения снаружи боту
type Message struct {
ChatId int64
Msg string
}
// We inherit the bot to rewrite the function for receiving updates
type Tlg32 struct {
botApi *tgbotapi.BotAPI
mode string
MyId int64
botName string
chatIds []int64
Flag bool
tokenPath string
token string
MsgChan chan Message
CmdToController chan string
}
func Tlg32Create(botName string, mode string, tokenPath string, myId int64, msgChan chan Message, cmdChan chan string) *Tlg32 {
bot := Tlg32{}
bot.mode = mode
bot.tokenPath = tokenPath
bot.botName = botName //your bot name
bot.MyId = myId
bot.chatIds = append(bot.chatIds, myId)
bot.Flag = true
bot.MsgChan = msgChan
bot.CmdToController = cmdChan
return &bot
}
func (bot *Tlg32) get_token() error {
tokenFileName := bot.tokenPath
token, err := os.ReadFile(tokenFileName)
if err != nil {
return err
}
// remove trailing CR LF SPACE
l := len(token)
c := token[l-1]
for c == 10 || c == 13 || c == 32 {
token = token[:l-1]
l = len(token)
c = token[l-1]
}
bot.token = string(token)
// log.Println("Bot token: ", bot.token)
return nil
}
func (bot *Tlg32) Stop() {
bot.Flag = false
log.Println("Telegram stopped")
}
func (bot *Tlg32) Run() error {
var err error
err = bot.get_token()
if err != nil {
return err
}
bot.botApi, err = tgbotapi.NewBotAPI(string(bot.token))
if err != nil {
return errors.New("incorrect token")
}
bot.botApi.Debug = false
log.Printf("Telebot authorized on account %s\n", bot.botApi.Self.UserName)
go func() {
bot.send_msg()
}()
go func() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
for bot.Flag {
updates := bot.My_get_updates_chan(u)
for update := range updates {
if !bot.Flag {
bot.botApi.StopReceivingUpdates()
return
}
if update.Message != nil { // If we got a message
var outMsg string
chatId := update.Message.Chat.ID
msgIn := update.Message.Text
firstName := update.Message.From.FirstName
fmt.Printf("[%s] %s \n", update.Message.From.FirstName, update.Message.Text)
outMsg, err := bot.handle_msg_in(msgIn, chatId, firstName)
if err != nil {
outMsg = "I'm understand you"
}
bot.MsgChan <- Message{ChatId: chatId, Msg: outMsg}
}
}
}
}()
// log.Println("Telegram bot started")
return nil
}
func (bot *Tlg32) send_msg() {
// Этот код включаем, если нужно цитирование принятого сообщения
// msg.ReplyToMessageID = update.Message.MessageID
for bot.Flag {
inMsg := <-bot.MsgChan
chatId := inMsg.ChatId
text := inMsg.Msg
msg := tgbotapi.NewMessage(chatId, text)
bot.botApi.Send(msg)
}
}
// GetUpdatesChan starts and returns a channel for getting updates.
func (bot *Tlg32) My_get_updates_chan(config tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel {
ch := make(chan tgbotapi.Update, bot.botApi.Buffer)
go func() {
for bot.Flag {
updates, err := bot.botApi.GetUpdates(config)
if err != nil {
log.Println(err)
log.Println("Failed to get updates, terminate...")
err_str := fmt.Sprintf("%s", err)
if strings.Contains(err_str, "Conflict:") {
bot.Flag = false
close(ch)
return
} else {
continue
}
}
for _, update := range updates {
if update.UpdateID >= config.Offset {
config.Offset = update.UpdateID + 1
ch <- update
}
}
}
}()
return ch
}
// Обработчик входящих сообщений
func (bot *Tlg32) handle_msg_in(msg string, chatId int64, firstName string) (string, error) {
// ID входит в список разрешенных
found := false
for _, acc := range bot.chatIds {
if chatId == acc {
found = true
break
}
}
// "/start" return "Привет, %s", message.from->first_name);
if strings.Contains(msg, "/start") {
return fmt.Sprintf("Привет, %s!", firstName), nil
}
if strings.Contains(msg, "/stop") {
// bot.Flag = false
return fmt.Sprintf("Good bye, %s!", firstName), nil
}
/*
// "/lwreg" add in chatIds
if strings.Contains(msg, "/lwreg") {
bot.chatIds = append(bot.chatIds, chatId)
return "Ok", nil
}
*/
if found {
//"/lwunreg" remove from chatIds
if strings.Contains(msg, "/lwunreg") {
for i, acc := range bot.chatIds {
if acc == chatId {
copy(bot.chatIds[i:], bot.chatIds[i+1:])
bot.chatIds = bot.chatIds[:len(bot.chatIds)-1]
break
}
}
return "Ok", nil
}
// "/cmnd" проверяем команду на список допустимых, если Ок, отправляем на выполнение
if strings.Contains(msg, "/cmnd") {
return bot.handle_command(msg), nil
}
return "Ok", nil
} else {
return "Фиг вам!", errors.New("запрос не принят")
}
}
// Пока жестко один пробел между параметрами команды
func (bot *Tlg32) handle_command(msg string) string {
cmnd := strings.Trim(msg, " ")
var cmd int
n, err := fmt.Sscanf(cmnd, "/cmnd %d", &cmd)
if n == 0 || err != nil || cmd < 400 || cmd > 499 {
return "Фиг вам!"
}
bot.CmdToController <- cmnd
return "Ok"
}