-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (132 loc) · 3.4 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"log"
"math/rand"
"github.com/jadow/Telegram-Dice-Bot/config"
"github.com/tkanos/gonfig"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
const (
configPath = "config/config.json"
dice4 = "4 sided dice"
dice6 = "6 sided dice"
dice8 = "8 sided dice"
dice10 = "10 sided dice"
dice12 = "12 sided dice"
dice20 = "20 sided dice"
help = "telegram bot for rolling dice\n/open to open keyboard\n/close to close keyboard"
openKeyboard = "roll dice?"
removeKeyboard = "removing keyboard"
)
var numericKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(dice4),
tgbotapi.NewKeyboardButton(dice6),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(dice8),
tgbotapi.NewKeyboardButton(dice10),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(dice12),
tgbotapi.NewKeyboardButton(dice20),
),
)
var diceStringInt = map[string]int{
dice4: 4,
dice6: 6,
dice8: 8,
dice10: 10,
dice12: 12,
dice20: 20,
}
var mapIntString = map[int]string{
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
}
func configure() config.Configuration {
// server details
c := &config.Configuration{}
err := gonfig.GetConf(configPath, c)
if err != nil {
log.Println("configuration error. unable to configure server")
}
return *c
}
func getName(user tgbotapi.User) string {
if user.UserName != "" {
return user.UserName
}
return user.FirstName
}
func getDice(randomLimit int) int {
return rand.Intn(randomLimit) + 1
}
func getDiceMessage(name string, random int) string {
return fmt.Sprintf("%s rolled a %d", name, random)
}
func getDiceRollMessage(name string, dice string, random int) string {
return fmt.Sprintf("%s rolled a %s and got a %d ", name, dice, random)
}
//identify using title
func getQuery(update tgbotapi.Update) tgbotapi.InlineConfig {
results := []interface{}{}
for i, v := range diceStringInt {
result := tgbotapi.NewInlineQueryResultArticle(i, i,
getDiceRollMessage(getName(*update.InlineQuery.From), i, getDice(v)))
result.Description = i
results = append(results, result)
}
return tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: false,
CacheTime: 0,
Results: results,
}
}
func getCommand(update tgbotapi.Update) tgbotapi.MessageConfig {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
if update.Message.IsCommand() {
switch update.Message.Command() {
case "start", "help":
msg.Text = help
case "open":
msg.Text = openKeyboard
msg.ReplyMarkup = numericKeyboard
case "close":
msg.Text = removeKeyboard
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
}
} else if randomLimit, ok := diceStringInt[update.Message.Text]; ok {
msg.Text = getDiceMessage(getName(*update.Message.From), getDice(randomLimit))
}
return msg
}
func main() {
config := configure()
bot, err := tgbotapi.NewBotAPI(config.Token)
if err != nil {
log.Panic(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
fmt.Print(".")
for update := range updates {
if update.InlineQuery != nil {
res := getQuery(update)
bot.AnswerInlineQuery(res)
} else if update.Message != nil {
msg := getCommand(update)
bot.Send(msg)
}
//else continue
}
}