/
main.go
85 lines (69 loc) · 1.97 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
package main
import (
"log"
"os"
"time"
"github.com/yanzay/tbot"
)
func main() {
token := os.Getenv("TELEGRAM_TOKEN")
// Create new telegram bot server using token
bot, err := tbot.NewServer(token)
if err != nil {
log.Fatal(err)
}
// Use whitelist for Auth middleware, allow to interact only with user1 and user2
whitelist := []string{"yanzay", "user2"}
bot.AddMiddleware(tbot.NewAuth(whitelist))
// Yo handler works without slash, simple text response
bot.Handle("yo", "YO!")
// Handle with HiHandler function
bot.HandleFunc("/hi", HiHandler)
// Handler can accept varialbes
bot.HandleFunc("/say {text}", SayHandler)
// Bot can send stickers, photos, music
bot.HandleFunc("/sticker", StickerHandler)
bot.HandleFunc("/photo", PhotoHandler)
bot.HandleFunc("/keyboard", KeyboardHandler)
// Use file handler to handle user uploads
bot.HandleFile(FileHandler)
// Set default handler if you want to process unmatched input
bot.HandleDefault(EchoHandler)
// Start listening for messages
err = bot.ListenAndServe()
log.Fatal(err)
}
func HiHandler(message *tbot.Message) {
// Handler can reply with several messages
message.Replyf("Hello, %s!", message.From)
time.Sleep(1 * time.Second)
message.Reply("What's up?")
}
func SayHandler(message *tbot.Message) {
// Message contain it's varialbes from curly brackets
message.Reply(message.Vars["text"])
}
func EchoHandler(message *tbot.Message) {
message.Reply(message.Text())
}
func StickerHandler(message *tbot.Message) {
message.ReplySticker("sticker.png")
}
func PhotoHandler(message *tbot.Message) {
message.ReplyPhoto("photo.jpg", "it's me")
}
func KeyboardHandler(message *tbot.Message) {
buttons := [][]string{
{"Some", "Test", "Buttons"},
{"Another", "Row"},
}
message.ReplyKeyboard("Buttons example", buttons)
}
func FileHandler(message *tbot.Message) {
err := message.Download("./uploads")
if err != nil {
message.Replyf("Error handling file: %q", err)
return
}
message.Reply("Thanks for uploading!")
}