Fuse is a Go library for developing Telegram bots, using the Telegram Bot API.
you@pc:~$ go get -u heytobi.dev/fuse
✔️ Register Webhooks
✔️ Receive updates through Webhooks
✔️ Receive updates through polling
✔️ Send Messages
✔️ Supports Local Bot API Servers
- Initialize a Bot
- Register command handlers
- Start polling for updates
httpClient := &http.Client{}
config := &telegram.Config{
Token: "<YOUR TELEGRAM TOKEN>",
UpdateMethod: telegram.UpdateMethodGetUpdates,
PollingIntervalMS: 1000,
PollingTimeout: 30,
PollingUpdatesLimit: 100,
}
poller, err := telegram.NewPoller(telegramConfig, httpClient)
if err != nil {
return nil, errors.New("failed to initialize telegram poller")
}
bot, err := telegram.NewBot(telegramConfig, httpClient)
if err != nil {
return nil, errors.New("failed to initialize telegram instance")
}
bot = bot.WithPoller(poller)
bot.RegisterHandler("/start", func(ctx context.Context, update *telegram.Update) {
result, err := bot.Send(telegram.SendMessageRequest{
ChatID: update.Message.Chat.ID,
Text: " ¯\_(ツ)_/¯",
})
if err != nil {
log.Error("failed to send telegram message")
}
if !result.Successful {
log.Warn(fmt.Sprintf("failed to send telegram message: %s", result.Description))
}
})
bot.Start() // start listening for updates.
- Initialize a Bot
- Register a Webhook
- Register command handlers
- Call the process update method directly whenever your webhook is invoked
httpClient := &http.Client{}
config := &telegram.Config{
Token: "<YOUR TELEGRAM TOKEN>",
UpdateMethod: telegram.UpdateMethodWebhook,
}
bot, err := telegram.Init(config, httpClient)
if err != nil {
log.Fatal("failed to initialize telegram bot")
}
bot.RegisterWebhook(telegram.Webhook{url: "mywebhook.com/notify"})
if err != nil {
log.Fatal("failed to register webhook")
}
bot.RegisterHandler("/start", func(ctx context.Context, update *telegram.Update) {
result, err := bot.Send(telegram.SendMessageRequest{
ChatID: update.Message.Chat.ID,
Text: " ¯\_(ツ)_/¯",
})
if err != nil {
log.Error("failed to send telegram message")
}
if !result.Successful {
log.Warn(fmt.Sprintf("failed to send telegram message: %s", result.Description))
}
})
// In your webhook http handler:
bot.ProcessUpdate(Update{}) // the update parameter should be deserialized from the request body.
If you are running a Local Bot API Server, you can specify the host and the port (if applicable) using the fields exposed in the config struct:
config := &telegram.Config{
BotApiServer: "https://localserver.net",
BotApiServerPort: 1234,
}
The full documentation with examples is available at fuse.heytobi.dev
MIT License
Copyright (c) 2022 Oluwatobi Adeyinka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.