Skip to content

Commit

Permalink
use new format
Browse files Browse the repository at this point in the history
  • Loading branch information
Divkix committed Feb 22, 2023
1 parent 38dabb8 commit 3e0fcdf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
12 changes: 4 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ var (
enableWebhook bool
webhookPort int
botToken string
apiUrl string
webhookUrl string
webhookDomain string
databaseUrl string
databaseName string
webhookSecret string
)

func init() {
flag.StringVar(&botToken, "BOT_TOKEN", os.Getenv("BOT_TOKEN"), "Bot token for running the bot")
flag.StringVar(&apiUrl, "API_URL", os.Getenv("API_URL"), "Api Server used to connect bot to")
flag.BoolVar(&enableWebhook, "USE_WEBHOOKS", func() bool {
return os.Getenv("USE_WEBHOOKS") == "yes" || os.Getenv("USE_WEBHOOKS") == "true"
}(),
"Enable webhooks",
)
flag.StringVar(&webhookUrl, "WEBHOOK_URL", os.Getenv("WEBHOOK_URL"), "URL for the Webhook")
flag.StringVar(&webhookSecret, "WEBHOOK_SECRET", os.Getenv("WEBHOOK_SECRET"), "Secret for webhook")
flag.StringVar(&webhookDomain, "WEBHOOK_DOMAIN", os.Getenv("WEBHOOK_DOMAIN"), "URL for the Webhook")
flag.IntVar(
&webhookPort,
"PORT",
Expand All @@ -46,8 +46,4 @@ func init() {

flag.StringVar(&databaseUrl, "DB_URI", os.Getenv("DB_URI"), "Database URI for MongoDB")
flag.StringVar(&databaseName, "DB_NAME", os.Getenv("DB_NAME"), "Bot database name in MongoDB")

if apiUrl == "" {
apiUrl = "https://api.telegram.org"
}
}
48 changes: 27 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ import (
)

func main() {
b, err := gotgbot.NewBot(
botToken,
&gotgbot.BotOpts{
DefaultRequestOpts: &gotgbot.RequestOpts{
Timeout: gotgbot.DefaultTimeout,
APIURL: apiUrl,
},
Client: http.Client{},
// Create bot from environment value.
b, err := gotgbot.NewBot(botToken, &gotgbot.BotOpts{
Client: http.Client{},
DefaultRequestOpts: &gotgbot.RequestOpts{
Timeout: gotgbot.DefaultTimeout,
APIURL: gotgbot.DefaultAPIURL,
},
)
})
if err != nil {
panic("failed to create new bot: " + err.Error())
}

// Create updater and dispatcher.
updater := ext.NewUpdater(nil)
updater := ext.NewUpdater(&ext.UpdaterOpts{
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: 0,
}),
})
dispatcher := updater.Dispatcher

// Handlers for running commands.
Expand All @@ -48,28 +55,27 @@ func main() {

if enableWebhook {
log.Println("[Webhook] Starting webhook...")
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!).
}

// Start the webhook
err = updater.StartWebhook(b,
botToken,
ext.WebhookOpts{
ListenAddr: "0.0.0.0:" + fmt.Sprint(webhookPort),
},
)
// We use the token as the urlPath for the webhook, as using a secret ensures that strangers aren't crafting fake updates.
err = updater.StartWebhook(b, botToken, webhookOpts)
if err != nil {
log.Fatalf("[Webhook] Failed to start webhook: %s", err.Error())
panic("failed to start webhook: " + err.Error())
}

// Set webhooks for all bots
err = updater.SetAllBotWebhooks(webhookUrl, &gotgbot.SetWebhookOpts{
err = updater.SetAllBotWebhooks(webhookDomain, &gotgbot.SetWebhookOpts{
MaxConnections: 100,
DropPendingUpdates: true,
SecretToken: webhookOpts.SecretToken,
})

if err != nil {
log.Fatalf("failed to set webhook: %s\n", err.Error())
} else {
log.Printf("[Webhook] Set Webhook to: %s\n", webhookUrl)
log.Printf("[Webhook] Set Webhook to: %s\n", webhookDomain)
}

log.Println("[Webhook] Webhook started Successfully!")
Expand Down

0 comments on commit 3e0fcdf

Please sign in to comment.