-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage main.go
362 lines (301 loc) · 14.6 KB
/
package 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"log"
"os"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
// Those are keyboards that appear for some of the questions
var numericKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("ERC20"),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("ERC20Votes")),
)
var yesNoKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("Yes"),
tgbotapi.NewKeyboardButton("No")),
)
var correctKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("Name")),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("Symbol"),
tgbotapi.NewKeyboardButton("Supply"),
tgbotapi.NewKeyboardButton("Type")),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("It's all correct"),
),
)
// to operate the bot, put a text file containing key for your bot acquired from telegram "botfather" to the same directory with this file
var tgApiKey, err = os.ReadFile(".secret")
var bot, error1 = tgbotapi.NewBotAPI(string(tgApiKey))
// type containing all the info about user input
type user struct {
id int64
status int64
exportTokenName string
exportTokenSymbol string
exportTokenSupply uint64
exportTokenType uint64
tokenTypeString string
}
// main database, key (int64) is telegram user id
var userDatabase = make(map[int64]user)
func main() {
bot, err = tgbotapi.NewBotAPI(string(tgApiKey))
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
//whenever bot gets a new message, check for user id in the database happens, if it's a new user, the entry in the database is created.
for update := range updates {
if update.Message != nil {
if _, ok := userDatabase[update.Message.From.ID]; !ok {
userDatabase[update.Message.From.ID] = user{update.Message.Chat.ID, 0, "", "", 0, 0, ""}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "Heya, wanna mint your own ERC20 Aor ERC20Votes? You've come to the right place! Let's begin. Tell me the name of your token!")
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
msg.ParseMode = "Markdown"
bot.Send(msg)
} else {
switch userDatabase[update.Message.From.ID].status {
//first check for user status, (for a new user status 0 is set automatically), then user reply for the first bot message is logged to a database as name AND user status is updated
case 0:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenName = update.Message.Text
updateDb.status = 1
userDatabase[update.Message.From.ID] = updateDb
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, userDatabase[update.Message.From.ID].exportTokenName+"? That's a cool name! Now tell me the symbol of your token? Usually it's like Bitcoin - BTC, you get the idea")
bot.Send(msg)
}
//logic is that 1 incoming message fro the user equals one status check in database, so each status check ends with the message asking the next question
case 1:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenSymbol = update.Message.Text
updateDb.status = 2
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, userDatabase[update.Message.From.ID].exportTokenSymbol+", alright. Now tell me, what's your desired supply of the tokens?")
bot.Send(msg)
//decimals asked, check if user input is uint, token type asked, keyboard is provided
case 2:
TokenSupplyString := update.Message.Text
tokenSupply, err2 := strconv.ParseUint(TokenSupplyString, 10, 64)
if err2 == nil && tokenSupply > 0 {
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenSupply = tokenSupply
updateDb.status = 3
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, TokenSupplyString+" tokens may exist at max, great. Now let's decide about what type of token you want to use - ERC20 or ERC20Votes?")
msg.ReplyMarkup = numericKeyboard
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "Please, enter a number of tokens you want to exist!")
bot.Send(msg)
}
//desired tokentype asked here, it is collected both as string and uint numbers. string is used inside this program, uint is exported. check message asked
case 3:
if update.Message.Text == "ERC20" || update.Message.Text == "ERC20Votes" {
var tokenType uint64
var tokenTypeString string
if update.Message.Text == "ERC20" {
tokenType = 0
tokenTypeString = "ERC20"
} else if update.Message.Text == "ERC20Votes" {
tokenType = 1
tokenTypeString = "ERC20Votes"
}
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenType = tokenType
updateDb.tokenTypeString = tokenTypeString
updateDb.status = 4
userDatabase[update.Message.From.ID] = updateDb
}
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
checkMsg := "Okay, let's check it.\n \n" +
"Token name: " + userDatabase[update.Message.From.ID].exportTokenName + "\n" +
"Token symbol: " + userDatabase[update.Message.From.ID].exportTokenSymbol + "\n" +
"Total supply: " + supplyString + "\n" +
"Token type: " + tokenTypeString + "\n \n" +
"Is this right?"
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, checkMsg)
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
msg.ReplyMarkup = yesNoKeyboard
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "That's not the type!")
bot.Send(msg)
}
//after check message is sent, keyboard is provided. if user answers yes, link to a front-end (WIP) is provided, his entry in the database is deleted, so
//next time the same user contacts the bot, the process will begin all over again
//any other answer than "yes" brings the options to correct the info
case 4:
if update.Message.Text == "Yes" || update.Message.Text == "It's all correct" {
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
typeString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenType, 10)
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "Here's the link to mint your token! \n http://localhost:3000/?name="+userDatabase[update.Message.From.ID].exportTokenName+"&symbol="+userDatabase[update.Message.From.ID].exportTokenSymbol+"&supply="+supplyString+"&type="+typeString)
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
bot.Send(msg)
delete(userDatabase, update.Message.From.ID)
} else if update.Message.Text == "No" {
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.status = 5
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "What needs to be corrected?")
msg.ReplyMarkup = correctKeyboard
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "That was a Yes/No question...")
bot.Send(msg)
}
//status 5-9 are used for data correction
case 5:
switch update.Message.Text {
case "Name":
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.status = 6
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "What's the correct name?")
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
bot.Send(msg)
case "Symbol":
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.status = 7
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "What's the correct symbol?")
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
bot.Send(msg)
case "Supply":
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.status = 8
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "What's the correct supply?")
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
bot.Send(msg)
case "Type":
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.status = 9
userDatabase[update.Message.From.ID] = updateDb
}
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "What's the correct type?")
msg.ReplyMarkup = numericKeyboard
bot.Send(msg)
//keyboard is provided, so whenever user input this one, the link is provided and user entry is deleted from the database
case "It's all correct":
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
typeString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenType, 10)
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "Here's the link to mint your token! \n http://localhost:3000/?name="+userDatabase[update.Message.From.ID].exportTokenName+"&symbol="+userDatabase[update.Message.From.ID].exportTokenSymbol+"&supply="+supplyString+"&type="+typeString)
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
bot.Send(msg)
delete(userDatabase, update.Message.From.ID)
default:
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Please, select what needs to be edited!")
bot.Send(msg)
}
//those are used to correct the data. after each correction status is set to 5 AND keyboard to select what needs to be edited is provided,
//so if something else needs to be corrected, it may be done infinitely, process terminates when the input is "it's all correct"
//name edit
case 6:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenName = update.Message.Text
updateDb.status = 5
userDatabase[update.Message.From.ID] = updateDb
}
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
checkMsgFinal := "Okay, let's check it.\n \n" +
"Token name: " + userDatabase[update.Message.From.ID].exportTokenName + "\n" +
"Token symbol: " + userDatabase[update.Message.From.ID].exportTokenSymbol + "\n" +
"Total supply: " + supplyString + "\n" +
"Token type: " + userDatabase[update.Message.From.ID].tokenTypeString + "\n \n" +
"What else needs to be corrected?"
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, checkMsgFinal)
msg.ReplyMarkup = correctKeyboard
bot.Send(msg)
//symbol edit
case 7:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenSymbol = update.Message.Text
updateDb.status = 5
userDatabase[update.Message.From.ID] = updateDb
}
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
checkMsgFinal := "Okay, let's check it.\n \n" +
"Token name: " + userDatabase[update.Message.From.ID].exportTokenName + "\n" +
"Token symbol: " + userDatabase[update.Message.From.ID].exportTokenSymbol + "\n" +
"Total supply: " + supplyString + "\n" +
"Token type: " + userDatabase[update.Message.From.ID].tokenTypeString + "\n \n" +
"What else needs to be corrected?"
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, checkMsgFinal)
msg.ReplyMarkup = correctKeyboard
bot.Send(msg)
//supply edit
case 8:
TokenSupplyString := update.Message.Text
tokenSupply, err2 := strconv.ParseUint(TokenSupplyString, 10, 64)
if err2 == nil && tokenSupply > 0 {
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenSupply = tokenSupply
updateDb.status = 5
userDatabase[update.Message.From.ID] = updateDb
}
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
checkMsgFinal := "Okay, let's check it.\n \n" +
"Token name: " + userDatabase[update.Message.From.ID].exportTokenName + "\n" +
"Token symbol: " + userDatabase[update.Message.From.ID].exportTokenSymbol + "\n" +
"Total supply: " + supplyString + "\n" +
"Token type: " + userDatabase[update.Message.From.ID].tokenTypeString + "\n \n" +
"What else needs to be corrected?"
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, checkMsgFinal)
msg.ReplyMarkup = correctKeyboard
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, "Please, enter a number of tokens you want to exist!")
bot.Send(msg)
}
//type edit
case 9:
if update.Message.Text == "ERC20" || update.Message.Text == "ERC20Votes" {
var tokenType uint64
var tokenTypeString string
if update.Message.Text == "ERC20" {
tokenType = 0
tokenTypeString = "ERC20"
} else if update.Message.Text == "ERC20Votes" {
tokenType = 1
tokenTypeString = "ERC20Votes"
}
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.exportTokenType = tokenType
updateDb.tokenTypeString = tokenTypeString
updateDb.status = 5
userDatabase[update.Message.From.ID] = updateDb
}
supplyString := strconv.FormatUint(userDatabase[update.Message.From.ID].exportTokenSupply, 10)
checkMsgFinal := "Okay, let's check it.\n \n" +
"Token name: " + userDatabase[update.Message.From.ID].exportTokenName + "\n" +
"Token symbol: " + userDatabase[update.Message.From.ID].exportTokenSymbol + "\n" +
"Total supply: " + supplyString + "\n" +
"Token type: " + userDatabase[update.Message.From.ID].tokenTypeString + "\n \n" +
"What else needs to be corrected?"
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].id, checkMsgFinal)
msg.ReplyMarkup = correctKeyboard
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "That's not the type!")
bot.Send(msg)
}
}
}
}
}
}