I can not figure out how to induce (trigger) a local server with a telegram bot to send a message to a channel.
For example, to send a notification to a user from the website, where he has registered and connected his telegram. We will assume that the user has already started dialog with bot and is ready to receive messages from it.
I have a separate server that can send requests to the server with the bot, but I cannot understand how to receive and handle requests on this telegram-bot server.
I'm using the go telegram-bot-api library and at this moment the server uses a long-polling approach instead of webhooks. So it receives some telegram API events through an update channel.
My code is just a copy of example in telegram-bot-api golang lib git repo:
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)
}
}
I expect my bot to be induced (triggered) from a third-party service, get some data from it, generate a message and send it to the user through the chat.
At this time my implementation is: bot gets messages from user by the update channel, handles it and sends a reply message.
The telegram-bot-api
sends messages to Telegram Bot API using HTTP POST requests.
See more here: https://github.com/go-telegram-bot-api/telegram-bot-api/blob/master/bot.go#L71
You can simply initialize another BotAPI
instance inside the server logic and use it to send messages asynchronously. It won't affect active long polling or webhooks.