R
R
runaway13382019-06-16 23:52:03
go
runaway1338, 2019-06-16 23:52:03

Telegram-bot: how to trigger sending a message?

I am writing a telegram bot in Golang using telegram-bot-api. The way to communicate with the telegram api is long-polling. There is a channel that receives update data from telegram (see code below). The task is to add the ability to trigger a bot (something like an endpoint) so that it sends a message to a channel with a specific chatId. For example, a request comes from another micro-service, in the body of which a userId comes, the bot searches the database for a user with the same userId, takes its chatId and sends a message there. Actually, I can’t understand how I can accept such a “trigger” on a server with a telegram bot. It might be better to use webhooks, but I still don't understand how to implement it there either. Could you tell me, please, what solutions are there for the task at hand?

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

    bot.Debug = true

    log.Printf("Authorized on account %s", bot.Self.UserName)

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

    updates, err := bot.GetUpdatesChan(u)

    for update := range updates {
        if update.Message == nil { // ignore any non-Message Updates
            continue
        }

        log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

        msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
        msg.ReplyToMessageID = update.Message.MessageID

        bot.Send(msg)
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bynov, 2019-06-17
@Bynov

Perhaps I misunderstood your question, but
Try to make a regular API in your application, when accessed, you can check the user against the database and send him a message via tgbotapi.NewMessage()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question