-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
84 lines (74 loc) · 1.78 KB
/
app.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
package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"net/http"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
)
func parseWebhookAnswer(resp *http.Response) (string, tgbotapi.Params) {
var method string
params := make(tgbotapi.Params)
var parsed map[string]interface{}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return method, params
}
err = json.Unmarshal(respBytes, &parsed)
if err != nil {
return method, params
}
for key, value := range parsed {
if key == "method" {
method = value.(string)
} else {
switch value := value.(type) {
case int64:
params.AddNonZero64(key, value)
case float64:
params.AddNonZeroFloat(key, value)
case string:
params.AddNonEmpty(key, value)
}
}
}
return method, params
}
func main() {
log.SetOutput(os.Stdout)
botToken := flag.String("token", "", "Telegram Bot Token")
webhookUrl := flag.String("url", "", "Bot WebHook url")
flag.Parse()
if *botToken == "" || *webhookUrl == "" {
log.Fatal("Usage: tg-webhook-emulator -token <TELEGRAM_BOT_TOKEN> -url <webhook_url>")
}
bot, err := tgbotapi.NewBotAPI(*botToken)
if err != nil {
log.Fatal(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
jsonValue, _ := json.Marshal(update)
resp, err := http.Post(*webhookUrl, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
log.Error(err)
continue
}
method, params := parseWebhookAnswer(resp)
apiResp, err := bot.MakeRequest(method, params)
if err != nil {
if apiResp != nil {
log.Errorf("error: %s, resp: %s", err, apiResp.Result)
} else {
log.Error(err)
}
continue
}
}
}