-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (95 loc) · 2.65 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/luism6n/calcbot/calc"
"gopkg.in/telegram-bot-api.v4"
)
var (
token *string
debug *bool
port *string
)
func main() {
bot, updates := setupBot()
for update := range updates {
if update.InlineQuery == nil {
continue
}
query := update.InlineQuery.Query
evaluation, err := calc.Evaluate(query)
var result tgbotapi.InlineQueryResultArticle
if err != nil {
result = newInlineQueryResultArticle(fmt.Sprintf("%s", err.Error()))
} else {
result = newInlineQueryResultArticle(fmt.Sprintf("%s ~> %f", query, evaluation))
}
config := newInlineConfig(update.InlineQuery.ID, result)
res, err := bot.AnswerInlineQuery(config)
if err != nil {
log.Printf("Error:\nerr: %s\nres: %+v\nquery: %s", err.Error(), res, query)
}
}
}
func setupBot() (*tgbotapi.BotAPI, tgbotapi.UpdatesChannel) {
readCommandLineFlags()
bot, err := tgbotapi.NewBotAPI(*token)
if err != nil {
die("Bot token is invalid: %s", *token)
}
bot.Debug = *debug
u, err := url.Parse("https://luis-calc-bot.herokuapp.com/" + *token)
if err != nil {
die("Parsing webhook URL failed: %s", err.Error())
}
_, err = bot.SetWebhook(tgbotapi.WebhookConfig{
URL: u,
})
if err != nil {
die("Error setting webhook: %s", err.Error())
}
updates := bot.ListenForWebhook("/" + *token)
log.Print("Listening at 0.0.0.0:" + *port)
go http.ListenAndServe("0.0.0.0:"+*port, nil)
return bot, updates
}
func readCommandLineFlags() {
debug = flag.Bool("debug", false, "If the bot should run in debug mode")
port = flag.String("port", "No port provided", "Port to listen for updates")
token = flag.String("token", "No token provided", "The bot token")
flag.Parse()
log.Printf("Read arguments.\ndebug: %t\ntoken: %s\nport: %s", *debug, *token, *port)
}
func newInlineQueryResultArticle(text string) tgbotapi.InlineQueryResultArticle {
return tgbotapi.InlineQueryResultArticle{
Type: "article",
ID: "only result",
Title: "Evaluation result",
Description: text,
InputMessageContent: tgbotapi.InputTextMessageContent{
Text: text,
},
}
}
func newInlineConfig(queryID string, onlyResult tgbotapi.InlineQueryResultArticle) tgbotapi.InlineConfig {
return tgbotapi.InlineConfig{
InlineQueryID: queryID,
Results: castToInterfaceSlice([]tgbotapi.InlineQueryResultArticle{onlyResult}),
CacheTime: 300,
}
}
func castToInterfaceSlice(iqra []tgbotapi.InlineQueryResultArticle) []interface{} {
s := make([]interface{}, len(iqra))
for i, v := range iqra {
s[i] = v
}
return s
}
func die(format string, a ...interface{}) {
fmt.Printf(format+"\n", a...)
os.Exit(1)
}