/gotbot

A generic telegram bot api client made with go.

Primary LanguageGoGNU General Public License v3.0GPL-3.0

gotbot

A generic telegram bot api client made with go.

Introduction

This package was a result of my first telegram bot implementation. When I started that project I planned to only be concerned with my bot's functionality and not the details of handling requests and responses from the telegram bot api server. But after browsing for telegram api client packages written with go, I wasn't satisfied with what I found and I finally decided it was better to write one myself.

Since the telegram bot api is very vast, only some of the functionalities are currently implemented. But a contribution from you would very much be appreciated to minimize this limitation as much as possible.

Getting started

in order to start using this package, use the following command to add this package to your project.

go get github.com/roskee/gotbot

once you have successfully installed the package, you can use the methods and models specified in the package to set up and manage your bot.

Basics

Assuming you have created your bot on telegram and have your token. you can use the NewBot function to connect your bot.
Learn more about creating your first bot

apiToken := os.Getenv("API_KEY")
bot := gotbot.NewBot(apiToken, gotbot.BotOptions{})

Then you can use the RegisterMethod to register your first command.
Learn more about commands

err := bot.RegisterMethod("start", "start command", func(update entity.Update) {
    fmt.Println("start command was sent")
    // do you thing... maybe reply to the user
})

Finally, you have to start your server to actually listen for user interactions with your bot. There are two mutually exclusive ways of doing this.

polling

you can call getUpdates method periodically to get all user interactions since your last call to it. your server will indefinitely (with the given interval) ask the telegram bot api for new updates.

err = bot.Poll(5 * time.Second, entity.UpdateConfig{})

webhook

you can also register a webhook url for the telegram bot api server to call whenever there is a new update. This method only works with these security configurations

webhook := entity.Webhook{
    URL: "https://myurl.com/updatesCallback",
}
err = bot.Listen(5000, webhook, entity.UpdateConfig{})

Best Wishes!
Kirubel Adamu