olebedev/go-tgbot

404s after latest update

zhulik opened this issue · 6 comments

After update to latest verson I have these errors:

polling updates: [POST /bot{token}/getUpdates][404] getUpdatesNotFound  &{Description:Not Found ErrorCode:404 Ok:false}

Seems like {token} in url is not replaced with real token

Hi @zhulik,
could you please provide a piece of code which produces this?

I have made method's update for getUpdates. It works at least for all my projects, based on this lib.

Client and router initialization

func newBot(telegramToken string, repository *repository) *tgbot.Router {
	ctx := context.Background()

	r := tgbot.New(ctx, telegramToken)

	r.Use(func(c *tgbot.Context) error {
		c.Path = c.Path + c.Text
		return nil
	})

	r.Bind("^/message/(?:.*)/text/start.*$", func(c *tgbot.Context) error {
		return startCommand(botContext{router: r, context: c, repository: repository})
	})
	r.Bind("^/message/(?:.*)/text/help.*$", func(c *tgbot.Context) error {
		return helpCommand(botContext{router: r, context: c, repository: repository})
	})
	r.Bind("^/message/(?:.*)/text/auth\\s*$", func(c *tgbot.Context) error {
		return authEmptyCommand(botContext{router: r, context: c, repository: repository})
	})
	r.Bind("^/message/(?:.*)/text/auth\\s+(\\w+)$", func(c *tgbot.Context) error {
		return authCommand(botContext{router: r, context: c, repository: repository})
	})

	return r
}

telegramToken here is not empty.

Polling

if err := mode.router.Poll(ctx, []models.AllowedUpdate{models.AllowedUpdateMessage}); err != nil {
			log.Println(errors.Wrap(err, "telegramBotMode.Start"))
		}

Router here is created by calling previous functions.

Have a look at this code:

package main

import (
	"context"
	"flag"
	"log"
	"os"

	tgbot "github.com/olebedev/go-tgbot"
	"github.com/olebedev/go-tgbot/client/messages"
	"github.com/olebedev/go-tgbot/models"
)

var token *string

func main() {
	token = flag.String("token", "", "telegram bot token")
	flag.Parse()

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	r := tgbot.New(ctx, *token)

	// setup global middleware
	r.Use(tgbot.Logger(os.Stdout))
	r.Use(tgbot.Recover)

	r.Use(func(c *tgbot.Context) error {
		c.Path += c.Text
		return nil
	})

	// Bind handler
	r.Bind(`^/message/(?:.*)/text/start(?:\s(.*))?$`, func(c *tgbot.Context) error {
		// send greeting message back
		message := "hi there what's up"
		_, err := r.Messages.SendMessage(
			messages.NewSendMessageParams().WithBody(&models.SendMessageBody{
				Text:   &message,
				ChatID: c.Update.Message.Chat.ID,
			}),
		)
		if err != nil {
			return err
		}
		return nil
	})

	if err := r.Poll(ctx, []models.AllowedUpdate{models.AllowedUpdateMessage}); err != nil {
		log.Fatal(err)
	}
}

I have just tested this plain example and it works well, here is the output:

△ github.com/olebedev/tgbot-test DEBUG=1 go run main.go -token="239997296:AAHvpkCWZ9iB7ykOqpVKOpxj-foobar"
POST /bot239997296:AAHvpkCWZ9iB7ykOqpVKOpxj-foobar/getUpdates HTTP/1.1
Host: api.telegram.org
User-Agent: Go-http-client/1.1
Content-Length: 45
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip

{"allowed_updates":["message"],"timeout":29}

HTTP/1.1 200 OK
Content-Length: 23
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
Connection: keep-alive
Content-Type: application/json
Date: Mon, 27 Nov 2017 11:36:20 GMT
Server: nginx/1.10.1
Strict-Transport-Security: max-age=31536000; includeSubdomains

{"ok":true,"result":[]}
POST /bot239997296:AAHvpkCWZ9iB7ykOqpVKOpxj-foobar/getUpdates HTTP/1.1
Host: api.telegram.org
User-Agent: Go-http-client/1.1
Content-Length: 45
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip

{"allowed_updates":["message"],"timeout":29}

^Csignal: interrupt

The token is modified

So, the lib handles token properly. Are you sure that your newBot func received proper token?

Yes, I have checked the token value inside newBot function. Will try to implement some test app for reproducing

@zhulik, I have faced with the same bug eventually, it seems go-openapi has changed and this func is not being invoked in the new version.
I will try to solve the issue ASAP, thanks for the report.


Update: here is the reason why the lib is broken for now

Here is the fix - 316859f