-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
124 lines (98 loc) · 3.32 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
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"log"
"net/http"
"github.com/slovnik/slovnik"
"strings"
"github.com/pkg/errors"
"gopkg.in/telegram-bot-api.v4"
)
// Bot type aggregate all bot logic
type Bot struct {
api *tgbotapi.BotAPI
// updates is a channel that's used to communicate with telegram server
updates <-chan tgbotapi.Update
// env contains environvent for the bot
env *Env
// client for the slovnik API
slovnikAPIClient *slovnik.Client
}
// CreateBot creates and initializes new bot
func CreateBot(env *Env) (*Bot, error) {
var err error
bot := Bot{env: env}
bot.slovnikAPIClient, err = slovnik.NewClient(bot.env.config.SlovnikURL)
if bot.api, err = tgbotapi.NewBotAPI(env.config.BotID); err != nil {
return nil, errors.Wrapf(err, "Bot API init with ID '%s' failed", env.config.BotID)
}
if len(env.config.WebhookURL) == 0 {
log.Println("WebhookURL environment variable is not set. Using polling.")
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
bot.updates, err = bot.api.GetUpdatesChan(u)
} else {
log.Printf("WebhookURL is set to '%s'. Using webhooks\n", env.config.WebhookURL)
webHookURL := fmt.Sprintf("%s/bot%s", env.config.WebhookURL, bot.api.Token)
webHook := tgbotapi.NewWebhook(webHookURL)
if _, err = bot.api.SetWebhook(webHook); err != nil {
return nil, errors.Wrapf(err, "SetWebhook (%s) failed", webHookURL)
}
bot.updates = bot.api.ListenForWebhook("/bot" + bot.api.Token)
go http.ListenAndServe("0.0.0.0:8080", nil)
}
return &bot, nil
}
// Listen start listening on message updates and calling provided handler for processing incoming messages
func (bot *Bot) Listen() {
for update := range bot.updates {
if update.Message != nil {
bot.handleMessage(&update)
} else if update.CallbackQuery != nil {
bot.handleCallbackQuery(&update)
}
}
}
func (bot *Bot) handleMessage(update *tgbotapi.Update) {
words, err := bot.slovnikAPIClient.Translate(update.Message.Text)
if err != nil {
log.Println(err)
}
messageText := bot.env.template.Translation(words)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, messageText)
msg.ParseMode = tgbotapi.ModeMarkdown
msg.ReplyMarkup = bot.addMessageKeyboard(words)
_, err = bot.api.Send(msg)
if err != nil {
log.Println(err)
}
}
func (bot *Bot) handleCallbackQuery(update *tgbotapi.Update) {
callbackData := update.CallbackQuery.Data
if strings.HasPrefix(callbackData, "phrases:") {
w := strings.TrimPrefix(callbackData, "phrases:")
words, err := bot.slovnikAPIClient.Translate(w)
messageText := bot.env.template.Phrases(words)
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, messageText)
msg.ParseMode = tgbotapi.ModeMarkdown
_, err = bot.api.Send(msg)
if err != nil {
log.Println(err)
}
editMsg := tgbotapi.NewEditMessageText(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.MessageID, bot.env.template.Translation(words))
editMsg.ReplyMarkup = nil
editMsg.ParseMode = tgbotapi.ModeMarkdown
_, err = bot.api.Send(editMsg)
}
}
func (bot *Bot) addMessageKeyboard(words []slovnik.Word) *tgbotapi.InlineKeyboardMarkup {
if words == nil || len(words) > 1 || len(words[0].Samples) <= 0 {
return nil
}
keyboard := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Фразы", "phrases:"+words[0].Word),
),
)
return &keyboard
}