@ -1,134 +1,131 @@
# Telebot
# Telebot
>Telebot is a convenient wrapper to Telegram Bots API, written in Golang .
>Telebot is a Telegram bot framework in Go .
[![GoDoc ](https://godoc.org/github.com/tucnak/telebot?status.svg )](https://godoc.org/github.com/tucnak/telebot)
[![GoDoc ](https://godoc.org/github.com/tucnak/telebot?status.svg )](https://godoc.org/github.com/tucnak/telebot)
[![Travis ](https://travis-ci.org/tucnak/telebot.svg?branch=master )](https://travis-ci.org/tucnak/telebot)
[![Travis ](https://travis-ci.org/tucnak/telebot.svg?branch=master )](https://travis-ci.org/tucnak/telebot)
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.
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
Telebot offers a convenient wrapper to Bots API, so you shouldn't even
care about networking at all. You may install it with
bother about networking at all. You may install it with
go get github.com/tucnak/telebot
go get github.com/tucnak/telebot
(after setting up your `GOPATH` properly).
(after setting up your `GOPATH` properly).
Since you are probably
We highly recommend you to keep your bot access token outside the code base,
hosting your bot in a public repository, we'll add an environment
preferably as an environmental variable:
variable for the token in this example. Please set it with
export BOT_TOKEN=< your token here >
export BOT_TOKEN=< your token here >
Take a look at a minimal functional bot setup:
Here is an example "helloworld" bot, written with telebot:
```go
```go
package main
package main
import (
import (
"log"
"log"
"time"
"os"
"os"
"time"
"github.com/tucnak/telebot"
"github.com/tucnak/telebot"
)
)
func main() {
func main() {
bot, err := telebot.NewBot(os.Getenv("BOT_TOKEN"))
bot, err := telebot.NewBot(os.Getenv("BOT_TOKEN"))
if err != nil {
if err != nil {
log.Fatalln(err)
log.Fatalln(err)
}
}
messages := make(chan telebot.Message )
messages := make(chan telebot.Message, 100 )
bot.Listen(messages, 1*time.Second)
bot.Listen(messages, 1*time.Second)
for message := range messages {
for message := range messages {
if message.Text == "/hi" {
if message.Text == "/hi" {
bot.SendMessage(message.Chat,
bot.SendMessage(message.Chat,
"Hello, "+message.Sender.FirstName+"!", nil)
"Hello, "+message.Sender.FirstName+"!", nil)
}
}
}
}
}
}
```
```
## Inline mode
## Inline mode
As of January 4, 2016, Telegram added inline mode support for bots. Here's
As of January 4, 2016, Telegram added inline mode support for bots.
a nice way to handle both incoming messages and inline queries in the meantime:
Telebot support inline mode in a fancy manner. Here's a nice way to handle both incoming messages and inline queries:
```go
```go
package main
package main
import (
import (
"log"
"log"
"time"
"time"
"os"
"os"
"github.com/tucnak/telebot"
"github.com/tucnak/telebot"
)
)
var bot *telebot.Bot
func main() {
func main() {
var err error
bot, err := telebot.NewBot(os.Getenv("BOT_TOKEN"))
bot, err = telebot.NewBot(os.Getenv("BOT_TOKEN"))
if err != nil {
if err != nil {
log.Fatalln(err)
log.Fatalln(err)
}
}
bot.Messages = make(chan telebot.Message, 10 00)
bot.Messages = make(chan telebot.Message, 1 00)
bot.Queries = make(chan telebot.Query, 1000)
bot.Queries = make(chan telebot.Query, 1000)
go messages( )
go messages(bot )
go queries( )
go queries(bot )
bot.Start(1 * time.Second)
bot.Start(1 * time.Second)
}
}
func messages() {
func messages(bot *telebot.Bot) {
for message := range bot.Messages {
for message := range bot.Messages {
log.Printf("Received a message from %s with the text: %s\n", message.Sender.Username, message.Text)
log.Printf("Received a message from %s with the text: %s\n",
}
message.Sender.Username, message.Text)
}
}
}
func queries() {
func queries(bot *telebot.Bot ) {
for query := range bot.Queries {
for query := range bot.Queries {
log.Println("--- new query ---")
log.Println("--- new query ---")
log.Println("from:", query.From.Username)
log.Println("from:", query.From.Username)
log.Println("text:", query.Text)
log.Println("text:", query.Text)
// Create an article (a link) object to show in our results.
// Create an article (a link) object to show in results.
article := & telebot.InlineQueryResultArticle{
article := & telebot.InlineQueryResultArticle{
Title: "Telegram bot framework written in Go ",
Title: "Telebot ",
URL: "https://github.com/tucnak/telebot",
URL: "https://github.com/tucnak/telebot",
InputMessageContent: & telebot.InputTextMessageContent{
InputMessageContent: & telebot.InputTextMessageContent{
Text: "Telebot is a convenient wrapper to Telegram Bots API, written in Golang .",
Text: "Telebot is a Telegram bot framework .",
DisablePreview: false,
DisablePreview: false,
},
},
}
}
// Build the list of results. In this instance, just our 1 article from above .
// Build the list of results (make sure to pass pointers!) .
results := []telebot.InlineQueryResult{article}
results := []telebot.InlineQueryResult{article}
// Build a response object to answer the query.
// Build a response object to answer the query.
response := telebot.QueryResponse{
response := telebot.QueryResponse{
Results: results,
Results: results,
IsPersonal: true,
IsPersonal: true,
}
}
// And finally send the response .
// Send it .
if err := bot.AnswerInlineQuery(& query, &response); err != nil {
if err := bot.AnswerInlineQuery(& query, &response); err != nil {
log.Println("Failed to respond to query:", err)
log.Println("Failed to respond to query:", err)
}
}
}
}
}
}
```
```
## Files
## Files
Telebot lets you upload files from the file system:
Telebot lets you upload files from the file system:
```go
```go
boom, err := telebot.NewFile("boom.ogg")
boom, err := telebot.NewFile("boom.ogg")
if err != nil {
if err != nil {
return err
return err
}
}
audio := telebot.Audio{File: boom}
audio := telebot.Audio{File: boom}
@ -139,22 +136,22 @@ err = bot.SendAudio(recipient, &audio, nil)
```
```
## Reply markup
## 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:
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:
```go
```go
// Send a selective force reply message.
// Send a selective force reply message.
bot.SendMessage(user, "pong", & telebot.SendOptions{
bot.SendMessage(user, "pong", & telebot.SendOptions{
ReplyMarkup: telebot.ReplyMarkup{
ReplyMarkup: telebot.ReplyMarkup{
ForceReply: true,
ForceReply: true,
Selective: true,
Selective: true,
CustomKeyboard: [][]string{
CustomKeyboard: [][]string{
[]string{"1", "2", "3"},
[]string{"1", "2", "3"},
[]string{"4", "5", "6"},
[]string{"4", "5", "6"},
[]string{"7", "8", "9"},
[]string{"7", "8", "9"},
[]string{"*", "0", "#"},
[]string{"*", "0", "#"},
},
},
},
},
},
},
)
)
```
```