Quick Start

Quick start with basics of creation of Telegram Bot.

Where to start?

If you are a complete beginner and/or have no experience with Telegram Bots or don’t know where to start, highly suggest first reading Bots: An introduction for developers in order to understand what bots can do. If you have questions about how to create/modify a bot or what is a bot token, that is also described in the introduction for developers.

If you are familiar with bots capabilities, but still don’t know how they work, you can read at least some parts of the API reference:

Starting with Telego

For a quick start, you can follow these simple steps and at the end you will have a simple “echo” bot that sends your messages back to you. From that you can go further exploring Telegram bots.

Get Telego with go get.

go get github.com/mymmrac/telego

Import Telego packages. More about handlers and utils.

import (
    "fmt"
    "os"

    "github.com/mymmrac/telego"
    th "github.com/mymmrac/telego/telegohandler"
    tu "github.com/mymmrac/telego/telegoutil"
)

Create a bot instance and specify optional settings. More about configuration options.

botToken := os.Getenv("TOKEN")

bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}

Please keep in mind that default logger may expose sensitive information, use in development only.

It’s not recommended to hardcode tokens, so the environment variable was used. Also, both error and debug logs were enabled.

Get and print bot info. More about methods.

botUser, err := bot.GetMe()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}

fmt.Printf("Bot user: %+v\n", botUser)

If everything was properly configured, you should see your bot user printed.

Get updates from Telegram via long polling (not recommend, more here).

updates, _ := bot.UpdatesViaLongPolling(nil)
// ...
defer bot.StopLongPolling()

Create bot handler, register new message handler and start handling updates. More about methods and handlers.

bh, _ := th.NewBotHandler(bot, updates)

bh.HandleMessage(func(bot *telego.Bot, message telego.Message) {
    chatID := tu.ID(message.Chat.ID)
    _, _ = bot.CopyMessage(
        tu.CopyMessage(chatID, chatID, message.MessageID),
    )
})

defer bh.Stop()
// ...

bh.Start()

Now you are done, after starting your bot you will see debug logs of updates that came to the bot and any sent messages to the bot will be sent back to you.

Full Code Example
package main

import (
    "fmt"
    "os"

    "github.com/mymmrac/telego"
    th "github.com/mymmrac/telego/telegohandler"
    tu "github.com/mymmrac/telego/telegoutil"
)

func main() {
    botToken := os.Getenv("TOKEN")

    bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    botUser, err := bot.GetMe()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Printf("Bot user: %+v\n", botUser)

    updates, _ := bot.UpdatesViaLongPolling(nil)

    bh, _ := th.NewBotHandler(bot, updates)

    bh.HandleMessage(func(bot *telego.Bot, message telego.Message) {
        chatID := tu.ID(message.Chat.ID)
        _, _ = bot.CopyMessage(
            tu.CopyMessage(chatID, chatID, message.MessageID),
        )
    })

    defer bh.Stop()
    defer bot.StopLongPolling()

    bh.Start()
}

Next steps

For more information, keep reading the next sections of docs. In tutorial, the same example will be reviewed more closely with different ways to do things.

Most of the things you will need to know can be found in a Telegram Bot API and/or in these docs, so just keep exploring.

You can also look at some other examples located here.