go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API
https://go-telegram-bot-api.dev
MIT License
5.76k stars 889 forks source link

No Examples on Creating Conversations #612

Closed AdamRussak closed 1 year ago

AdamRussak commented 1 year ago

i created some flow, but can't find how to bind it to a specific conversation flow

in python, there are many examples and "how-to"s to create a telegram conversation.

I am missing this. for example, the flow below works, but it isn't strict to a conversation flow.

func main() {
    bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
    if err != nil {
        log.Panic(err)
    }

    bot.Debug = true

    log.Info("Authorized on account " + bot.Self.UserName)

    u := tgbotapi.NewUpdate(0)
    u.Timeout = 30

    updates := bot.GetUpdatesChan(u)
    // Loop through each update.
    var imdbInfo *imdb.Title
    for update := range updates {
        if CheckAlowedID(bot, update) {
            var msg tgbotapi.Chattable
            // Check if we've gotten a message update.
            if update.Message != nil {
                isImdbLink, imdbID := core.ValidateImdbLink(update.Message.Command())
                if update.Message.Command() == "/start" {
                    inputText := fmt.Sprintf(openMessage, update.Message.Chat.FirstName)
                    msg = tgbotapi.NewMessage(update.Message.Chat.ID, inputText)
                } else if isImdbLink {
                    msg, imdbInfo = core.ImdbGetItem(imdbID, update)
                } else {
                    continue
                }
                sendMessage(bot, msg)
            } else if update.CallbackQuery != nil {
                log.Info(update.CallbackQuery.Data + " was pressed")
                if update.CallbackQuery.Data == "yes" {
                    r := media.CheckIfItemExist(imdbInfo.Type, imdbInfo.Name, imdbInfo.ID)
                    if r.TotalRecordCount == 1 {
                        inputText := core.ViperConfigVariable("EMBY_URL") + "/web/index.html#!/item?id=" + r.Items[0].Id + "&serverId=" + r.Items[0].ServerId
                        msg := tgbotapi.NewMessage(update.CallbackQuery.From.ID, inputText)
                        sendMessage(bot, msg)
                    }
                }
            }
        }
    }
}
ikiselewskii commented 1 year ago

you need to store conversation states for different users somewhere and check user`s state on every update, idk what python library did you use but its the same in aiogram

AdamRussak commented 1 year ago

thank you! do you have some kind of documentation of example for me to go by?

ikiselewskii commented 1 year ago

here's a module that implements FSM in go, though I would recommend writing it yourself, you can use Redis(or similar key-value storage) to store chat_id as key and state as value Just check for a state of every event you process