-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers.go
More file actions
42 lines (34 loc) · 851 Bytes
/
handlers.go
File metadata and controls
42 lines (34 loc) · 851 Bytes
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
package main
import (
"encoding/json"
"io/ioutil"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
func (app *appStruct) webhookHandler(c *gin.Context) {
defer c.Request.Body.Close()
bytes, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Println(err)
return
}
var update tgbotapi.Update
err = json.Unmarshal(bytes, &update)
if err != nil {
log.Println(err)
return
}
// Do nothing if message is nil
if update.Message == nil {
return
}
// to monitor changes run: heroku logs --tail
log.Printf("From: %+v Text: %+v", update.Message.From, update.Message.Text)
app.handleMessage(&update)
}
func (app *appStruct) handleMessage(update *tgbotapi.Update) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
msg.Text = "Hello, mate!"
app.sendMsg(msg)
}