2015-06-25 19:43:19 +00:00
# Telebot
2015-06-27 11:30:35 +00:00
>Telebot is a convenient wrapper to Telegram Bots API, written in Golang.
2015-06-25 19:43:19 +00:00
2015-06-27 15:44:12 +00:00
[![GoDoc ](https://godoc.org/github.com/tucnak/telebot?status.svg )](https://godoc.org/github.com/tucnak/telebot)
2015-10-15 14:57:47 +00:00
[![Travis ](https://travis-ci.org/tucnak/telebot.svg?branch=master )](https://travis-ci.org/tucnak/telebot)
2015-06-27 15:44:12 +00:00
2015-06-27 11:30:35 +00:00
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.
2015-07-03 16:56:35 +00:00
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:
2015-06-27 11:30:35 +00:00
```go
2016-07-02 11:04:42 +00:00
package main
2015-06-27 11:30:35 +00:00
import (
"time"
"github.com/tucnak/telebot"
)
func main() {
2015-07-03 18:54:40 +00:00
bot, err := telebot.NewBot("SECRET TOKEN")
2015-06-27 11:30:35 +00:00
if err != nil {
2016-06-26 14:05:37 +00:00
log.Fatalln(err)
2015-06-27 11:30:35 +00:00
}
messages := make(chan telebot.Message)
bot.Listen(messages, 1*time.Second)
for message := range messages {
if message.Text == "/hi" {
bot.SendMessage(message.Chat,
2015-07-06 13:46:19 +00:00
"Hello, "+message.Sender.FirstName+"!", nil)
2015-06-27 11:30:35 +00:00
}
}
}
```
2016-01-22 11:38:45 +00:00
## Inline mode
2016-06-26 14:05:37 +00:00
As of January 4, 2016, Telegram added inline mode support for bots.
Telebot support inline mode in a fancy manner.
Here's a nice way to handle both incoming messages and inline queries:
2016-01-22 11:38:45 +00:00
```go
2016-07-02 11:04:42 +00:00
package main
2016-01-22 11:38:45 +00:00
import (
2016-06-26 14:05:37 +00:00
"log"
2016-01-22 11:38:45 +00:00
"time"
"github.com/tucnak/telebot"
)
var bot *telebot.Bot
func main() {
2016-07-02 11:04:42 +00:00
var err error
bot, err = telebot.NewBot("SECRET TOKEN")
2016-06-26 14:05:37 +00:00
if err != nil {
log.Fatalln(err)
}
2016-01-22 11:38:45 +00:00
2016-06-26 14:05:37 +00:00
bot.Messages = make(chan telebot.Message, 1000)
bot.Queries = make(chan telebot.Query, 1000)
2016-01-22 11:38:45 +00:00
2016-06-26 14:05:37 +00:00
go messages()
go queries()
2016-01-22 11:38:45 +00:00
bot.Start(1 * time.Second)
}
func messages() {
2016-06-26 14:05:37 +00:00
for message := range bot.Messages {
2016-07-02 11:04:42 +00:00
log.Printf("Received a message from %s with the text: %s\n", message.Sender.Username, message.Text)
2016-06-26 14:05:37 +00:00
}
2016-01-22 11:38:45 +00:00
}
func queries() {
2016-06-26 14:05:37 +00:00
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 our results.
article := & telebot.InlineQueryResultArticle{
Title: "Telegram bot framework written in Go",
URL: "https://github.com/tucnak/telebot",
InputMessageContent: & telebot.InputTextMessageContent{
Text: "Telebot is a convenient wrapper to Telegram Bots API, written in Golang.",
DisablePreview: false,
},
}
// Build the list of results. In this instance, just our 1 article from above.
results := []telebot.InlineQueryResult{article}
// Build a response object to answer the query.
response := telebot.QueryResponse{
Results: results,
IsPersonal: true,
}
// And finally send the response.
if err := bot.AnswerInlineQuery(& query, &response); err != nil {
log.Println("Failed to respond to query:", err)
}
}
2016-01-22 11:38:45 +00:00
}
```
## Files
2015-07-03 16:56:35 +00:00
2016-01-22 11:38:45 +00:00
Telebot lets you upload files from the file system:
2015-07-03 16:56:35 +00:00
```go
boom, err := telebot.NewFile("boom.ogg")
if err != nil {
return err
}
2015-08-31 21:30:13 +00:00
audio := telebot.Audio{File: boom}
// Next time you send & audio, telebot won't issue
2015-07-03 16:56:35 +00:00
// an upload, but would re-use existing file.
2015-08-31 21:30:13 +00:00
err = bot.SendAudio(recipient, & audio, nil)
2015-07-06 13:46:19 +00:00
```
2016-01-22 11:38:45 +00:00
## Reply markup
2015-07-06 13:46:19 +00:00
2016-01-22 11:38:45 +00:00
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:
2015-07-06 13:46:19 +00:00
```go
// Send a selective force reply message.
bot.SendMessage(user, "pong", & telebot.SendOptions{
2015-11-03 15:12:22 +00:00
ReplyMarkup: telebot.ReplyMarkup{
ForceReply: true,
2015-07-06 13:46:19 +00:00
Selective: true,
2016-01-22 11:38:45 +00:00
CustomKeyboard: [][]string{
[]string{"1", "2", "3"},
[]string{"4", "5", "6"},
[]string{"7", "8", "9"},
[]string{"*", "0", "#"},
},
2015-07-06 13:46:19 +00:00
},
},
)
2015-07-03 16:56:35 +00:00
```