Skip to content

zoni/telebot

This branch is 68 commits behind tucnak/telebot:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

12182e3 · Jun 26, 2016

History

73 Commits
Jun 25, 2015
Oct 16, 2015
Jun 25, 2015
Jan 22, 2016
Apr 29, 2016
Apr 29, 2016
Oct 15, 2015
Jun 26, 2016
Jun 26, 2016
Apr 13, 2016
Apr 29, 2016
Jul 6, 2015
Jun 26, 2016
May 25, 2016

Repository files navigation

Telebot

Telebot is a convenient wrapper to Telegram Bots API, written in Golang.

GoDoc Travis

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

Telebot offers a convenient wrapper to Bots API, so you shouldn't even care about networking at all. Here is an example "helloworld" bot, written with telebot:

import (
    "time"
    "github.com/tucnak/telebot"
)

func main() {
    bot, err := telebot.NewBot("SECRET TOKEN")
    if err != nil {
        return
    }

    messages := make(chan telebot.Message)
    bot.Listen(messages, 1*time.Second)

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

Inline mode

As of January 4, 2016, Telegram added inline mode support for bots. Telebot does support inline mode in a fancy manner. Here's a nice way to handle both incoming messages and inline queries:

import (
	"log"
    "time"

    "github.com/tucnak/telebot"
)

var bot *telebot.Bot

func main() {
    if newBot, err := telebot.NewBot("SECRET TOKEN"); err != nil {
        return
    } else {
		// shadowing, remember?
		bot = newBot
	}

	bot.Messages = make(chan telebot.Message, 1000)
	bot.Queries = make(chan telebot.Query, 1000)

	go messages()
	go queries()

    bot.Start(1 * time.Second)
}

func messages() {
	for message := range bot.Messages {
		// ...
	}
}

func queries() {
	for query := range bot.Queries {
		log.Println("--- new query ---")
		log.Println("from:", query.From)
		log.Println("text:", query.Text)

		// There you build a slice of let's say, article results:
		results := []telebot.Result{...}

		// And finally respond to the query:
		if err := bot.Respond(query, results); err != nil {
			log.Println("ouch:", err)
		}
	}
}

Files

Telebot lets you upload files from the file system:

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

audio := telebot.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", &telebot.SendOptions{
        ReplyMarkup: telebot.ReplyMarkup{
            ForceReply: true,
            Selective: true,

			CustomKeyboard: [][]string{
				[]string{"1", "2", "3"},
				[]string{"4", "5", "6"},
				[]string{"7", "8", "9"},
				[]string{"*", "0", "#"},
			},
        },
    },
)

About

Telegram bot framework written in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%