Go to file
Ian Byrd e5d60e6913
Replace IQR*.MarshalJSON with InlineQueryResultBase
This commit gets rid of multiple redundant copies of MarhsalJSON()
method present for all existing IQR. It does so by introducing a
new struct called InlineQueryResultBase, which takes care of new
Type (for JSON) and existing duplicated ID field.

InlineQueryResults is a new helper wrapper for []IQR slices. It
implements MarshalJSON, which makes sure IQR.ID and IQR.Type are
set properly (by computing a hash and infering IQR type).
2017-08-03 01:30:37 +03: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 Switching to int64 update queries, resolves #16 hopefully. 2016-11-10 01:34:57 +02:00
bot.go Refactoring, poor BC, closes #44 and probably resolves #41. 2016-11-10 22:04:50 +02:00
file.go GetFile and GetFileDirectURL method to fetch url of file received from user (#63) 2016-09-27 15:04:13 +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
message.go Fixes lots of complete bollocks that got into the codebase. 2016-11-10 21:34:02 +02:00
options.go Refactoring, poor BC, closes #44 and probably resolves #41. 2016-11-10 22:04:50 +02:00
README.md README.md bikeshedding. 2016-11-10 21:32:23 +02:00
telebot_test.go Minor documentation fixes, resolves #49 2016-06-26 10:15:11 +03:00
telebot.go Refactoring, poor BC, closes #44 and probably resolves #41. 2016-11-10 22:04:50 +02:00
types.go Refactoring, poor BC, closes #44 and probably resolves #41. 2016-11-10 22:04:50 +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 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 bother about networking at all. You may install it with

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 as an environmental variable:

export BOT_TOKEN=<your token here>

Take a look at a minimal functional bot setup:

package main

import (
	"log"
	"os"
	"time"

	"github.com/tucnak/telebot"
)

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

	messages := make(chan telebot.Message, 100)
	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. Here's a nice way to handle both incoming messages and inline queries in the meantime:

package main

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

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

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

	go messages(bot)
	go queries(bot)

	bot.Start(1 * time.Second)
}

func messages(bot *telebot.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 *telebot.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 := &telebot.InlineQueryResultArticle{
			Title: "Telebot",
			URL:   "https://github.com/tucnak/telebot",
			InputMessageContent: &telebot.InputTextMessageContent{
				Text:		   "Telebot is a Telegram bot framework.",
				DisablePreview: false,
			},
		}

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

		// Build a response object to answer the query.
		response := telebot.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:

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", "#"},
			},
		},
	},
)