-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (105 loc) · 3.04 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"log"
"os"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/enescakir/emoji"
)
var helpMenu = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Help", "help"),
),
)
var generateJoke = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Joke", "joke"),
),
)
func main() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("GOLANGTZBOT"))
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
go singleUpdate(update, bot)
}
}
func singleUpdate(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
if update.CallbackQuery != nil {
responseMsg := tgbotapi.NewCallbackWithAlert(update.CallbackQuery.ID, "")
receivedData := update.CallbackData()
switch receivedData {
case "help":
responseMsg.Text = "Coming Soon"
if _, err := bot.Request(responseMsg); err != nil {
log.Panic(err)
}
case "joke":
responseMsg.Text = Joke()
responseMsg.CacheTime = 10
if _, err := bot.Request(responseMsg); err != nil {
log.Panic(err)
}
}
return
}
if update.Message.NewChatMembers != nil {
deleteRequest := tgbotapi.NewDeleteMessage(update.Message.Chat.ID, update.Message.MessageID)
if _, err := bot.Request(deleteRequest); err != nil {
log.Panic(err)
}
member, _ := bot.GetChatMembersCount(tgbotapi.ChatMemberCountConfig{ChatConfig: update.FromChat().ChatConfig()})
welcomeText := fmt.Sprintf("Karibu %v %v. You are member number %v", update.Message.From.FirstName, emoji.WavingHand, member)
welcomeMsg := tgbotapi.NewMessage(update.Message.Chat.ID, welcomeText)
welcomeMsg.ReplyMarkup = helpMenu
sentMsg, err := bot.Send(welcomeMsg)
if err != nil {
log.Panic(err)
}
time.Sleep(60 * time.Second)
deleteMsg := tgbotapi.NewDeleteMessage(sentMsg.Chat.ID, sentMsg.MessageID)
if _, err := bot.Request(deleteMsg); err != nil {
log.Panic(err)
}
return
}
if update.Message.LeftChatMember != nil {
deleteRequest := tgbotapi.NewDeleteMessage(update.Message.Chat.ID, update.Message.MessageID)
if _, err := bot.Request(deleteRequest); err != nil {
log.Panic(err)
}
goodbyeMsg := tgbotapi.NewMessage(update.Message.Chat.ID, "Another fallen soldier")
if _, err := bot.Send(goodbyeMsg); err != nil {
log.Panic(err)
}
return
}
if !update.Message.IsCommand() {
return
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
switch update.Message.Command() {
case "help":
msg.Text = "So far I can only make Jokes. Use /joke"
msg.ReplyMarkup = helpMenu
case "hi":
msg.Text = "Hello there! :)"
case "status":
msg.Text = "I'm incomplete :("
case "joke":
msg.Text = "Tap the button to get random programming jokes"
msg.ReplyMarkup = generateJoke
default:
msg.Text = "I don't know that command"
}
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
}