diff options
| author | Shulhan <ms@kilabit.info> | 2024-04-17 00:54:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-04-17 00:54:29 +0700 |
| commit | f319122b736346c6e99d3d2165d018fb61de7faa (patch) | |
| tree | a6d2bc4f673c89a65d7a72ef2d283e6579f71691 /api/telegram | |
| parent | a1b5275b85b05b6aba46cf431a79919746157bfc (diff) | |
| download | pakakeh.go-f319122b736346c6e99d3d2165d018fb61de7faa.tar.xz | |
telegram/bot: register GET endpoint to test webhook
The call to get "GET <Webhook.URL.Path>/<Token>" will return HTTP status
200 with JSON body '{"code":200,"message":"OK"}'.
This endpoint is to check if the bot server is really running.
Diffstat (limited to 'api/telegram')
| -rw-r--r-- | api/telegram/bot/bot.go | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/api/telegram/bot/bot.go b/api/telegram/bot/bot.go index 38078878..6c0b8bc4 100644 --- a/api/telegram/bot/bot.go +++ b/api/telegram/bot/bot.go @@ -11,6 +11,7 @@ import ( "fmt" "log" "net/http" + "net/url" "path" "strconv" "time" @@ -414,6 +415,8 @@ func (bot *Bot) startWebhook() (err error) { // createServer start the HTTP server for receiving Update. func (bot *Bot) createServer() (err error) { + var logp = `createServer` + var serverOpts = libhttp.ServerOptions{ Address: bot.opts.Webhook.ListenAddress, } @@ -434,20 +437,42 @@ func (bot *Bot) createServer() (err error) { bot.webhook, err = libhttp.NewServer(serverOpts) if err != nil { - return fmt.Errorf("createServer: %w", err) + return fmt.Errorf(`%s: %w`, logp, err) } - var epToken = libhttp.Endpoint{ + var webhookURL *url.URL + + webhookURL, err = url.Parse(bot.opts.Webhook.URL) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + var fullPath = path.Join(webhookURL.Path, bot.opts.Token) + + var ep = libhttp.Endpoint{ + Method: libhttp.RequestMethodGet, + Path: fullPath, + RequestType: libhttp.RequestTypeNone, + ResponseType: libhttp.ResponseTypeJSON, + Call: bot.handleWebhookGet, + } + + err = bot.webhook.RegisterEndpoint(ep) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + ep = libhttp.Endpoint{ Method: libhttp.RequestMethodPost, - Path: "/" + bot.opts.Token, + Path: fullPath, RequestType: libhttp.RequestTypeJSON, ResponseType: libhttp.ResponseTypeNone, Call: bot.handleWebhook, } - err = bot.webhook.RegisterEndpoint(epToken) + err = bot.webhook.RegisterEndpoint(ep) if err != nil { - return fmt.Errorf("createServer: %w", err) + return fmt.Errorf(`%s: %w`, logp, err) } return nil @@ -476,6 +501,17 @@ func (bot *Bot) handleWebhook(epr *libhttp.EndpointRequest) (resBody []byte, err return resBody, nil } +func (bot *Bot) handleWebhookGet(_ *libhttp.EndpointRequest) (resBody []byte, err error) { + var res = libhttp.EndpointResponse{} + res.Code = 200 + res.Message = `OK` + resBody, err = json.Marshal(&res) + if err != nil { + return nil, err + } + return resBody, nil +} + func (bot *Bot) handleUpdateCommand(update Update) bool { ok := update.Message.parseCommandArgs() if !ok { |
