-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
main.go
154 lines (129 loc) · 3.77 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
package main
import (
"os"
"time"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
func main() {
var (
// Universal markup builders.
menu = &tb.ReplyMarkup{ResizeReplyKeyboard: true}
// Reply buttons.
btnHelp = menu.Text("ℹ Help")
btnSettings = menu.Text("⚙ Settings")
)
menu.Reply(
menu.Row(btnHelp),
menu.Row(btnSettings),
)
b, err := tb.NewBot(tb.Settings{
// You can also set custom API URL.
// If field is empty it equals to "https://api.telegram.org".
// URL: "http://195.129.111.17:8012",
Token: os.Getenv("TELEGRAM_BOT_TOKEN"),
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
// Synchronous: false,
Verbose: false,
// ParseMode: "",
// Reporter: nil,
// Client: nil,
// Offline: false,
})
if err != nil {
log.Fatal(err)
return
}
// Command: /start <PAYLOAD>
b.Handle("/start", func(m *tb.Message) {
if !m.Private() {
return
}
b.Send(m.Sender, "Hello!", menu)
})
// On reply button pressed (message)
b.Handle(&btnHelp, func(m *tb.Message) {
log.Infof("btnHelp: %#v", m)
var (
// Inline buttons.
//
// Pressing it will cause the client to
// send the bot a callback.
//
// Make sure Unique stays unique as per button kind,
// as it has to be for callback routing to work.
//
selector = &tb.ReplyMarkup{}
btnPrev = selector.Data("⬅", "prev", "data1", "data2")
btnNext = selector.Data("➡", "next", "data1", "data2")
)
selector.Inline(
selector.Row(btnPrev, btnNext),
)
// On inline button pressed (callback)
b.Handle(&btnPrev, func(c *tb.Callback) {
// Always respond!
b.Respond(c, &tb.CallbackResponse{
Text: "callback response",
ShowAlert: false,
// URL: "",
})
})
b.Send(m.Sender, "help button clicked", selector)
})
b.Handle("/hello", func(m *tb.Message) {
log.Infof("/hello %#v", m)
// b.Send(m.Sender, "Hello World!")
})
b.Handle(tb.OnText, func(m *tb.Message) {
log.Infof("[onText] %#v", m)
// all the text messages that weren't
// captured by existing handlers
})
b.Handle(tb.OnQuery, func(q *tb.Query) {
log.Infof("[onQuery] %#v", q)
// r := &tb.ReplyMarkup{}
// r.URL("test", "https://media.tenor.com/images/f176705ae1bb3c457e19d8cd71718ac0/tenor.gif")
urls := []string{
// "https://media.tenor.com/images/aae0cdf3c5a291cd7b96432180f6eee3/tenor.png",
// "https://media.tenor.com/images/905c1a9b1f56ae3c458b1ef58fd46357/tenor.png",
"https://media.tenor.com/images/2e69768f9537957ed3015a80ebc3f0f1/tenor.gif",
"https://media.tenor.com/images/6fcd72b29127a55e5c35db86d06d665c/tenor.gif",
"https://media.tenor.com/images/05dbf5bf3a3b88275bb045691541dc53/tenor.gif",
"https://media.tenor.com/images/0e1a52cfe5616c1509090d6ec2312db0/tenor.gif",
"https://media.tenor.com/images/1ca04a449b26e1f7d45682a79d2c8697/tenor.gif",
"https://media.tenor.com/images/a2844b186fb71c376226b56c4ea7730a/tenor.gif",
"https://media.tenor.com/images/ec636a1ebce1a3fc1c795b851c125b31/tenor.gif",
"https://media.tenor.com/images/ae103819cb05a0cf7497900b77b87d80/tenor.gif",
}
results := make(tb.Results, len(urls)) // []tb.Result
for i, url := range urls {
// result := &tb.PhotoResult{
result := &tb.GifResult{
ResultBase: tb.ResultBase{
// Type: "photo",
// Content: nil,
// ReplyMarkup: nil,
ID: uuid.New().String(),
},
URL: url,
// required for photos
ThumbURL: url,
}
results[i] = result
// needed to set a unique string ID for each result
// results[i].SetResultID(strconv.Itoa(i))
}
err := b.Answer(q, &tb.QueryResponse{
QueryID: q.ID,
Results: results,
// CacheTime: 60, // a minute
IsPersonal: true,
})
if err != nil {
log.Println(err)
}
})
b.Start()
}