-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.go
129 lines (112 loc) · 2.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
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
package main
import (
"fmt"
"net/mail"
"os"
"strconv"
"sync"
"github.com/mymmrac/telego"
th "github.com/mymmrac/telego/telegohandler"
tu "github.com/mymmrac/telego/telegoutil"
)
// State of the user
type State uint
const (
StateDefault State = iota
StateAskName
StateAskAge
StateAskEmail
StateConfirm
)
// User data and state
type User struct {
State State
Name string
Age uint
Email string
}
func main() {
botToken := os.Getenv("TOKEN")
// Create Bot with debug on
// Note: Please keep in mind that default logger may expose sensitive information, use in development only
bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Get updates channel
updates, _ := bot.UpdatesViaLongPolling(nil)
// Create bot handler
bh, _ := th.NewBotHandler(bot, updates)
// Create user storage (for this simple exampled in-memory map is sufficient,
// but in the real world you might want to use persistent storage like PostgreSQL or Redis)
users := make(map[int64]User)
// Since this is in-memory storage, we must use mutex
lock := sync.RWMutex{}
// Handle any message
bh.HandleMessage(func(bot *telego.Bot, msg telego.Message) {
userID := msg.From.ID
lock.RLock()
user := users[userID]
lock.RUnlock()
var text string
switch user.State {
case StateDefault:
// Welcome message for new users
text = "Hello stranger, what's your name?"
user.State = StateAskName
case StateAskName:
// Specify name (no validation)
user.Name = msg.Text
user.State = StateAskAge
text = "How old are you?"
case StateAskAge:
// Specify age (validate that its positive number)
var age uint64
age, err = strconv.ParseUint(msg.Text, 10, 32)
if err != nil || age == 0 {
text = "Invalid age, please try again"
// No state change
} else {
user.Age = uint(age)
user.State = StateAskEmail
text = "What's your email?"
}
case StateAskEmail:
// Specify email (validate that its valid email address)
var addr *mail.Address
addr, err = mail.ParseAddress(msg.Text)
if err != nil {
text = "Invalid email, please try again"
// No state change
} else {
user.Email = addr.Address
user.State = StateConfirm
text = fmt.Sprintf(
"Your name is %s, your age is %d, and your email is %s, all corrent? (Y/N)",
user.Name, user.Age, user.Email,
)
}
case StateConfirm:
if msg.Text == "Y" {
text = "Thanks for your data!"
user.State = StateDefault
// Do some logic
} else {
text = "Ok, let's start again"
user.State = StateDefault
}
default:
panic("unknown state")
}
lock.Lock()
users[userID] = user
lock.Unlock()
_, _ = bot.SendMessage(tu.Message(msg.Chat.ChatID(), text))
})
// Stop handling updates on exit
defer bh.Stop()
defer bot.StopLongPolling()
// Start handling updates
bh.Start()
}