forked from go-telegram-bot-api/telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathwebhook_echo.go
More file actions
65 lines (49 loc) · 1.33 KB
/
webhook_echo.go
File metadata and controls
65 lines (49 loc) · 1.33 KB
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
package main
import (
"log"
"net/http"
api "github.com/OvyFlash/telegram-bot-api"
)
// func main() { webhook_echo() }
func webhook_echo() {
bot, err := api.NewBotAPI("MyAwesomeBotToken")
if err != nil {
panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// Listen for updates from the webhook
updates := bot.ListenForWebhook("/" + bot.Token)
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
// Set the webhook
webHook, err := api.NewWebhookWithCert("https://your-bot-host-url/"+bot.Token, api.FilePath("cert.pem"))
if err != nil {
panic(err)
}
apiResponse, err := bot.Request(webHook)
if err != nil {
panic(err)
}
if apiResponse.Ok {
log.Printf("Webhook set successfully")
} else {
log.Printf("Failed to set webhook: %s", apiResponse.Description)
}
info, err := bot.GetWebhookInfo()
if err != nil {
panic(err)
}
if info.LastErrorDate != 0 {
log.Printf("Failed to get webhook info: %s", info.LastErrorMessage)
}
for update := range updates {
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
// Send an echo reply to the message
msg := api.NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyParameters.MessageID = update.Message.MessageID
bot.Send(msg)
}
}