XUtils

telebot

Telegram bot framework is written in Go.


Telebot

Telebot is a Telegram bot framework in Go.

GoDoc Travis

NOTE: We are currently working on completing both v1 and v2 of our API. You can track the progress here and here.

Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or via group chats / channels. These accounts serve as an interface to your code.

Telebot offers a pretty convenient interface to Bots API and uses default HTTP client. Ideally, you wouldn’t need to worry about actual networking at all.

go get github.com/tucnak/telebot

(after setting up your GOPATH properly).

We highly recommend you to keep your bot access token outside the code base, preferably in an environmental variable:

export BOT_TOKEN=<your token here>

Take a look at a minimal functional bot setup:

package main

import (
	"log"
	"os"
	"time"

	tb "gopkg.in/tucnak/telebot.v1"
)

func main() {
	bot, err := tb.NewBot(os.Getenv("BOT_TOKEN"))
	if err != nil {
		log.Fatalln(err)
	}

	messages := make(chan tb.Message, 100)
	bot.Listen(messages, 10 * time.Second)

	for message := range messages {
		if message.Text == "/hi" {
			bot.SendMessage(message.Chat,
				"Hello, "+message.Sender.FirstName+"!", nil)
		}
	}
}

Files

Telebot lets you upload files from the file system:

boom, err := tb.NewFile("boom.ogg")
if err != nil {
	return err
}

audio := tb.Audio{File: boom}

// Next time you send &audio, telebot won't issue
// an upload, but would re-use existing file.
err = bot.SendAudio(recipient, &audio, nil)

Reply markup

Sometimes you wanna send a little complicated messages with some optional parameters. The third argument of all Send* methods accepts telebot.SendOptions, capable of defining an advanced reply markup:

// Send a selective force reply message.
bot.SendMessage(user, "pong", &tb.SendOptions{
		ReplyMarkup: tb.ReplyMarkup{
			ForceReply: true,
			Selective: true,
			CustomKeyboard: [][]string{
				[]string{"1", "2", "3"},
				[]string{"4", "5", "6"},
				[]string{"7", "8", "9"},
				[]string{"*", "0", "#"},
			},
		},
	},
)

Articles

  • coming soon...