-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (87 loc) · 2.11 KB
/
main.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
package main
import (
"fmt"
tgbotapi "github.com/crocone/tg-bot"
"github.com/joho/godotenv"
"log"
"os"
)
var bot *tgbotapi.BotAPI
type button struct {
name string
data string
}
func startMenu() tgbotapi.InlineKeyboardMarkup {
states := []button{
{
name: "Привет",
data: "hi",
},
{
name: "Пока",
data: "buy",
},
}
buttons := make([][]tgbotapi.InlineKeyboardButton, len(states))
for index, state := range states {
buttons[index] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(state.name, state.data))
}
return tgbotapi.NewInlineKeyboardMarkup(buttons...)
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal(".env not loaded")
}
bot, err = tgbotapi.NewBotAPI(os.Getenv("TG_API_BOT_TOKEN"))
if err != nil {
log.Fatalf("Failed to initialize Telegram bot API: %v", err)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
if err != nil {
log.Fatalf("Failed to start listening for updates %v", err)
}
for update := range updates {
if update.CallbackQuery != nil {
callbacks(update)
} else if update.Message.IsCommand() {
commands(update)
} else {
// simply message
}
}
}
func callbacks(update tgbotapi.Update) {
data := update.CallbackQuery.Data
chatId := update.CallbackQuery.From.ID
firstName := update.CallbackQuery.From.FirstName
lastName := update.CallbackQuery.From.LastName
var text string
switch data {
case "hi":
text = fmt.Sprintf("Привет %v %v", firstName, lastName)
case "buy":
text = fmt.Sprintf("Пока %v %v", firstName, lastName)
default:
text = "Неизвестная команда"
}
msg := tgbotapi.NewMessage(chatId, text)
sendMessage(msg)
}
func commands(update tgbotapi.Update) {
command := update.Message.Command()
switch command {
case "start":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Выберите действие")
msg.ReplyMarkup = startMenu()
msg.ParseMode = "Markdown"
sendMessage(msg)
}
}
func sendMessage(msg tgbotapi.Chattable) {
if _, err := bot.Send(msg); err != nil {
log.Panicf("Send message error: %v", err)
}
}