-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
66 lines (57 loc) · 1.38 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
package main
import (
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"log"
)
type Bot struct {
chatService Chat
botAPI *tgbotapi.BotAPI
botConfig tgbotapi.UpdateConfig
}
// NewBot returns new Bot service
func NewBot(b *tgbotapi.BotAPI, u tgbotapi.UpdateConfig, c Chat) Bot {
return Bot{
chatService: c,
botAPI: b,
botConfig: u,
}
}
func (b *Bot) listenMessages() {
updates, err := b.botAPI.GetUpdatesChan(b.botConfig)
if err != nil {
log.Panic(err)
}
for update := range updates {
message := update.Message
if message.Text != ""{
log.Printf("[%s] %s", message.From.UserName, message.Text)
}
text := b.MessageParser(message)
msg := tgbotapi.NewMessage(message.Chat.ID, text)
msg.ReplyToMessageID = message.MessageID
_, err := b.botAPI.Send(msg)
if err != nil {
log.Printf("ERROR: bot send failed %v", err)
}
}
}
func (b *Bot) SendToChats(chats map[int64]struct{}, msg string) (err error) {
for id := range chatMap {
msg := tgbotapi.NewMessage(id, msg)
m, err := b.botAPI.Send(msg)
if err != nil {
return fmt.Errorf("msg: %v, err: %v", m, err)
}
}
return
}
func (b *Bot) MessageParser(msg *tgbotapi.Message) string {
switch msg.Text {
case Start:
b.chatService.AddChatToPull(msg.Chat.ID)
return "Started listening the news! Wait for new post to arive! And remember... Roman Loves YOU! <3"
default:
return msg.Text
}
}