Go to file
Ian Byrd d5e4082eaf
New Edit() and EditCaption() methods are taking over.
IMO, we shouldn't be introducing distinct EditText/EditMarkup
methods because it just doesn't make sense, you can do both in
one and EditCaption is a pretty odd case so we live it be.
2017-11-19 03:44:31 +02:00
.gitignore Initial commit 2015-06-25 22:27:50 +03:00
.travis.yml Merging #20 into tucnak:master from aladine:patch-3 2015-10-16 19:36:20 +03:00
api.go Sending photos by URL. 2017-11-18 20:33:20 +02:00
bot.go New Edit() and EditCaption() methods are taking over. 2017-11-19 03:44:31 +02:00
callbacks.go Massive refactoring and file structure changes. 2017-11-18 15:06:20 +02:00
chat.go Telebot now compiles! :-) 2017-11-18 16:44:57 +02:00
editable.go Telebot now compiles! :-) 2017-11-18 16:44:57 +02:00
file.go Good news Edit() now works for all Editable text messages! 2017-11-18 20:47:04 +02:00
handlers.go Message should be passed by-value. 2017-08-19 18:58:10 +03:00
inline_article.go Improve support for inline queries 2016-06-26 16:33:16 +02:00
inline_types.go Documentation for newly introduced methods. 2016-11-10 20:05:52 +02:00
inline.go Typo infereIQR -> inferIQR. 2016-11-10 22:10:37 +02:00
input_types.go Fixes lots of complete bollocks that got into the codebase. 2016-11-10 21:34:02 +02:00
LICENSE Initial commit 2015-06-25 22:27:50 +03:00
media.go Sending photos by URL. 2017-11-18 20:33:20 +02:00
message.go New Edit() and EditCaption() methods are taking over. 2017-11-19 03:44:31 +02:00
options.go Telebot now compiles! :-) 2017-11-18 16:44:57 +02:00
README.md Refining ReplyMarkup API. 2017-11-17 15:55:18 +02:00
sendable.go Sending photos by URL. 2017-11-18 20:33:20 +02:00
stickers.go Telebot now compiles! :-) 2017-11-18 16:44:57 +02:00
telebot_test.go New Edit() and EditCaption() methods are taking over. 2017-11-19 03:44:31 +02:00
telebot.go Telebot now compiles! :-) 2017-11-18 16:44:57 +02:00
util.go New Edit() and EditCaption() methods are taking over. 2017-11-19 03:44:31 +02:00

Telebot

Telebot is a Telegram bot framework in Go.

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 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 gopkg.in/tucnak/telebot.v2

(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.v2"
)

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 msg := range messages {
		if msg.Text == "/hi" {
			bot.Send(msg.Chat, "Hello, "+msg.Sender.FirstName+"!")
		}
	}
}

Inline mode

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

package main

import (
	"log"
	"time"
	"os"

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

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

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

	go messages(bot)
	go queries(bot)

	bot.Start(10 * time.Second)
}

func messages(bot *tb.Bot) {
	for message := range bot.Messages {
		log.Printf("Received a message from %s with the text: %s\n",
			message.Sender.Username, message.Text)
	}
}

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

		// Create an article (a link) object to show in results.
		article := &tb.InlineQueryResultArticle{
			Title: "Telebot",
			URL:   "https://github.com/tucnak/telebot",
			InputMessageContent: &tb.InputTextMessageContent{
				Text:		   "Telebot is a Telegram bot framework.",
				DisablePreview: false,
			},
		}

		// Build the list of results (make sure to pass pointers!).
		results := []tb.InlineQueryResult{article}

		// Build a response object to answer the query.
		response := tb.QueryResponse{
			Results:	results,
			IsPersonal: true,
		}

		// Send it.
		if err := bot.AnswerInlineQuery(&query, &response); err != nil {
			log.Println("Failed to respond to query:", err)
		}
	}
}

Files

Telebot lets you upload files from the file system:

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

audio := &tb.Audio{File: f}

// Next time you'll be sending this very *Audio, Telebot won't
// re-upload the same file but rather use the copy from the
// server.
err = bot.Send(recipient, audio)

Reply markup

// Send a selective force reply message.
bot.Send(user, "pong", &tb.ReplyMarkup{
    ForceReply: true,
    Selective: true,

    ReplyKeyboard: keys,
})