forked from PaulSonOfLars/gotgbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (85 loc) · 3.71 KB
/
main.go
File metadata and controls
98 lines (85 loc) · 3.71 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
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
package main
import (
"fmt"
"log"
"os"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/message"
)
// This bot repeats everything you say - but it uses webhooks instead of long polling.
// Webhooks are slightly more complex to run, since they require a running webserver, as well as an HTTPS domain.
// For development purposes, we recommend running this with a tool such as ngrok (https://ngrok.com/).
// Simply install ngrok, make an account on the website, and run:
// `ngrok http 8080`
// Then, copy-paste the HTTPS URL obtained from ngrok (changes every time you run it), and run the following command
// from the samples/echoWebhookBot directory:
// `TOKEN="<your_token_here>" WEBHOOK_DOMAIN="<your_domain_here>" WEBHOOK_SECRET="<random_string_here>" go run .`
// Then, simply send /start to your bot; if it replies, you've successfully set up webhooks!
func main() {
// Get token from the environment variable.
token := os.Getenv("TOKEN")
if token == "" {
panic("TOKEN environment variable is empty")
}
// Get the webhook domain from the environment variable.
webhookDomain := os.Getenv("WEBHOOK_DOMAIN")
if webhookDomain == "" {
panic("WEBHOOK_DOMAIN environment variable is empty")
}
// Get the webhook secret from the environment variable.
webhookSecret := os.Getenv("WEBHOOK_SECRET")
if webhookSecret == "" {
panic("WEBHOOK_SECRET environment variable is empty")
}
// Create bot from environment value.
b, err := gotgbot.NewBot(token, nil)
if err != nil {
panic("failed to create new bot: " + err.Error())
}
// Create updater and dispatcher.
dispatcher := ext.NewDispatcher(&ext.DispatcherOpts{
// If an error is returned by a handler, log it and continue going.
Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
log.Println("an error occurred while handling update:", err.Error())
return ext.DispatcherActionNoop
},
MaxRoutines: ext.DefaultMaxRoutines,
})
updater := ext.NewUpdater(dispatcher, nil)
// Add echo handler to reply to all text messages.
dispatcher.AddHandler(handlers.NewMessage(message.Text, echo))
// Start the webhook server. We start the server before we set the webhook itself, so that when telegram starts
// sending updates, the server is already ready.
webhookOpts := ext.WebhookOpts{
ListenAddr: "localhost:8080", // This example assumes you're in a dev environment running ngrok on 8080.
SecretToken: webhookSecret, // Setting a webhook secret here allows you to ensure the webhook is set by you (must be set here AND in SetWebhook!).
}
// The bot's urlPath can be anything. Here, we use "custom-path/<TOKEN>" as an example.
// It can be a good idea for the urlPath to contain the bot token, as that makes it very difficult for outside
// parties to find the update endpoint (which would allow them to inject their own updates).
err = updater.StartWebhook(b, "custom-path/"+token, webhookOpts)
if err != nil {
panic("failed to start webhook: " + err.Error())
}
err = updater.SetAllBotWebhooks(webhookDomain, &gotgbot.SetWebhookOpts{
MaxConnections: 100,
DropPendingUpdates: true,
SecretToken: webhookOpts.SecretToken,
})
if err != nil {
panic("failed to set webhook: " + err.Error())
}
log.Printf("%s has been started...\n", b.User.Username)
// Idle, to keep updates coming in, and avoid bot stopping.
updater.Idle()
}
// echo replies to a messages with its own contents.
func echo(b *gotgbot.Bot, ctx *ext.Context) error {
_, err := ctx.EffectiveMessage.Reply(b, ctx.EffectiveMessage.Text, nil)
if err != nil {
return fmt.Errorf("failed to echo message: %w", err)
}
return nil
}