Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question