-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
71 lines (60 loc) · 1.32 KB
/
api.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
package main
import (
"fmt"
"os"
"reflect"
tgbotapi "github.com/Syfaro/telegram-bot-api"
)
type BotApi struct {
isDebug bool
bot *tgbotapi.BotAPI
chatID int64
adapterChan chan string
}
func redirect(api *BotApi, from tgbotapi.UpdatesChannel) {
for {
update := <-from
if update.Message == nil {
continue
}
if update.Message.From.UserName != "makxenov" {
fmt.Println("Unexpected user: " + update.Message.From.UserName)
continue
}
// Make sure that message is text
if reflect.TypeOf(update.Message.Text).Kind() == reflect.String && update.Message.Text != "" {
api.chatID = update.Message.Chat.ID
api.adapterChan <- update.Message.Text
} else {
api.send("Send command")
}
}
}
func (api *BotApi) getUpdatesChan() chan string {
api.adapterChan = make(chan string)
if !api.isDebug {
u := tgbotapi.NewUpdate(0)
updates, err := api.bot.GetUpdatesChan(u)
_check(err)
go redirect(api, updates)
}
return api.adapterChan
}
func (api *BotApi) init() {
if !api.isDebug {
var err error
api.bot, err = tgbotapi.NewBotAPI(os.Getenv("TOKEN"))
_check(err)
}
}
func (api *BotApi) send(text string) {
if !api.isDebug {
msg := tgbotapi.NewMessage(api.chatID, text)
_, e := api.bot.Send(msg)
if e != nil {
fmt.Println(e.Error())
}
} else {
fmt.Println(text)
}
}