/
main.go
89 lines (77 loc) · 1.82 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
// main.go
package main
import (
"encoding/json"
"fmt"
telegram "github.com/Syfaro/telegram-bot-api"
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"regexp"
"strings"
)
const (
BOTNAME string = ""
BOTAPIKEY string = ""
)
// list of actions
var (
actions map[string]func(*gin.Context, *telegram.Update) error = map[string]func(*gin.Context, *telegram.Update) error{
"echo": actionEcho,
"start": actionStart,
"dict": actionDictionary,
}
bot *telegram.BotAPI
)
func main() {
// prepare host:port to listen to
bind := fmt.Sprintf("%s:%s", "", "8000")
// just predefine the err variable to avoid some problems
var err error
// create an instance of Telegram Bot Api
bot, err = telegram.NewBotAPI(BOTAPIKEY)
if err != nil {
log.Panic(err)
}
// Compile the regexpression to match /action@botname
actionSeprator := regexp.MustCompile(`^\/[\da-zA-z@]+`)
// prepare the Gin Router
router := gin.Default()
// on request
router.POST("/", func(c *gin.Context) {
buf, err := ioutil.ReadAll(c.Request.Body)
update := telegram.Update{}
json.Unmarshal(buf, &update)
if err != nil {
c.String(500, err.Error())
return
}
// Extracting action name from text
botName := ""
act := actionSeprator.FindString(update.Message.Text)
actLength := len(act)
atsignPos := strings.Index(act, "@")
if atsignPos != -1 {
botName = act[atsignPos+1:]
act = act[:atsignPos]
}
if botName != "" && botName != BOTNAME {
c.String(200, "Wrong bot")
return
}
act = strings.TrimPrefix(act, "/")
act = strings.ToLower(act)
update.Message.Text = update.Message.Text[actLength:]
// check if the requested action exist or not
_, has := actions[act]
if has {
err = actions[act](c, &update)
if err != nil {
c.String(500, err.Error())
return
}
}
c.String(200, "done")
})
router.Run(bind)
}