-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
112 lines (94 loc) · 3.09 KB
/
sample.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
package main
import (
"context"
"os"
"os/signal"
"fmt"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
// Send any text message to the bot after the bot has been started
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(handler),
}
b, err := bot.New("5684161024:AAHHpI6R9JtM3EnN-ycZbdbUncTDBBOrD1s", opts...)
if err != nil {
panic(err)
}
b.RegisterHandler(bot.HandlerTypeMessageText, "/start", bot.MatchTypeExact, helloHandler)
b.Start(ctx)
}
type BotState int
const (
Registration BotState = iota
CreatePoll
EnterWalletID
Vote
)
var global_state = Registration;
func helloHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Hello, *" + bot.EscapeMarkdown(update.Message.From.FirstName) + "*",
ParseMode: models.ParseModeMarkdown,
})
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Please, register: *" + bot.EscapeMarkdown(update.Message.From.FirstName) + "*",
ParseMode: models.ParseModeMarkdown,
})
kb := &models.InlineKeyboardMarkup{
InlineKeyboard: [][]models.InlineKeyboardButton{
{
{Text: "Yes", CallbackData: "yes"},
{Text: "No", CallbackData: "no"},
},
},
}
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Do you already have a Nil-wallet?",
ReplyMarkup: kb,
})
}
func send_error(ctx context.Context, b *bot.Bot, id int64) {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: id,
Text: fmt.Sprintf("I am confused :("),
})
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
fmt.Println("handler begin")
if global_state == Registration {
b.AnswerCallbackQuery(ctx, &bot.AnswerCallbackQueryParams{
CallbackQueryID: update.CallbackQuery.ID,
ShowAlert: true,
})
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
Text: fmt.Sprintf("Selected options: %v", update.CallbackQuery.Data),
})
if update.CallbackQuery.Data == "yes" {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
Text: "Your wallet ID?",
})
global_state = EnterWalletID
} else {
// TODO
}
}else if global_state == EnterWalletID {
if update.Message == nil {
send_error(ctx, b, update.CallbackQuery.Message.Message.Chat.ID);
return
}
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: fmt.Sprintf("Your wallet ID: %v", update.Message.Text),
})
}
fmt.Println("handler end")
}