-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (62 loc) · 1.86 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
package main
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/ophel1ac/daily-tarot-card-bot/model"
"github.com/ophel1ac/daily-tarot-card-bot/utils"
"log"
"math/rand"
"time"
)
var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("🔮", "button_get_random_card"),
),
)
func main() {
bot, err := tgbotapi.NewBotAPI(utils.GetBotKey())
if err != nil {
log.Fatal(err)
}
bot.Debug = true
updateConfig := tgbotapi.NewUpdate(0)
updateConfig.Timeout = 30
updates := bot.GetUpdatesChan(updateConfig)
for update := range updates {
// Check if we've gotten a message update.
if update.Message != nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "[WIP] This bot sends you random tarot card.")
msg.ReplyMarkup = inlineKeyboard
// Send the message.
if _, err = bot.Send(msg); err != nil {
panic(err)
}
} else if update.CallbackQuery != nil {
// Respond to the callback query, telling Telegram to show the user
// a message with the data received.
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data)
if _, err := bot.Request(callback); err != nil {
panic(err)
}
// Send the message.
// And finally, send a message containing the data received.
switch update.CallbackQuery.Data {
case "button_get_random_card":
getRandomCardHandler(bot, &update)
}
}
}
}
func getRandomCardHandler(b *tgbotapi.BotAPI, update *tgbotapi.Update) {
rand.Seed(time.Now().UnixNano())
v := rand.Intn(22)
card := model.GetCard("default", v)
img := card.GetImage()
msg := tgbotapi.NewPhoto(update.CallbackQuery.Message.Chat.ID, tgbotapi.FileBytes{
Name: "default.jpg",
Bytes: img,
})
msg.Caption = card.Name + "\n" + card.MeaningUp
if _, err := b.Send(msg); err != nil {
panic(err)
}
}