telebot/README.md

149 lines
3.3 KiB
Markdown
Raw Normal View History

2015-06-25 19:43:19 +00:00
# Telebot
2016-11-10 19:31:32 +00:00
>Telebot is a Telegram bot framework in Go.
2015-06-25 19:43:19 +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)
2017-11-17 06:41:48 +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
via group chats / channels. These accounts serve as an interface to your code.
2017-11-17 06:41:48 +00:00
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.
2017-11-17 07:22:16 +00:00
go get gopkg.in/tucnak/telebot.v2
(after setting up your `GOPATH` properly).
2016-11-10 19:31:32 +00:00
We highly recommend you to keep your bot access token outside the code base,
2017-11-17 06:41:48 +00:00
preferably in an environmental variable:
export BOT_TOKEN=<your token here>
2016-11-10 19:31:32 +00:00
Take a look at a minimal functional bot setup:
2015-06-27 11:30:35 +00:00
```go
package main
2015-06-27 11:30:35 +00:00
import (
2016-11-10 19:31:32 +00:00
"log"
"os"
"time"
2017-11-17 07:22:16 +00:00
tb "gopkg.in/tucnak/telebot.v2"
2015-06-27 11:30:35 +00:00
)
func main() {
2017-11-17 06:41:48 +00:00
bot, err := tb.NewBot(os.Getenv("BOT_TOKEN"))
2016-11-10 19:31:32 +00:00
if err != nil {
log.Fatalln(err)
}
2017-11-17 06:41:48 +00:00
messages := make(chan tb.Message, 100)
bot.Listen(messages, 10 * time.Second)
2016-11-10 19:31:32 +00:00
2017-11-17 07:22:16 +00:00
for msg := range messages {
if msg.Text == "/hi" {
bot.Send(msg.Chat, "Hello, "+msg.Sender.FirstName+"!")
2016-11-10 19:31:32 +00:00
}
}
2015-06-27 11:30:35 +00:00
}
```
## Inline mode
2016-11-10 19:31:32 +00:00
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:
```go
package main
import (
2016-11-10 19:31:32 +00:00
"log"
"time"
"os"
2017-11-17 06:41:48 +00:00
2017-11-17 07:22:16 +00:00
tb "gopkg.in/tucnak/telebot.v2"
)
func main() {
2017-11-17 06:41:48 +00:00
bot, err := tb.NewBot(os.Getenv("BOT_TOKEN"))
2016-11-10 19:31:32 +00:00
if err != nil {
log.Fatalln(err)
}
2017-11-17 06:41:48 +00:00
bot.Messages = make(chan tb.Message, 100)
bot.Queries = make(chan tb.Query, 1000)
2016-11-10 19:31:32 +00:00
go messages(bot)
go queries(bot)
2017-11-17 06:41:48 +00:00
bot.Start(10 * time.Second)
}
2017-11-17 06:41:48 +00:00
func messages(bot *tb.Bot) {
2016-11-10 19:31:32 +00:00
for message := range bot.Messages {
log.Printf("Received a message from %s with the text: %s\n",
message.Sender.Username, message.Text)
}
}
2017-11-17 06:41:48 +00:00
func queries(bot *tb.Bot) {
2016-11-10 19:31:32 +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 results.
2017-11-17 06:41:48 +00:00
article := &tb.InlineQueryResultArticle{
2016-11-10 19:31:32 +00:00
Title: "Telebot",
URL: "https://github.com/tucnak/telebot",
2017-11-17 06:41:48 +00:00
InputMessageContent: &tb.InputTextMessageContent{
2016-11-10 19:31:32 +00:00
Text: "Telebot is a Telegram bot framework.",
DisablePreview: false,
},
}
// Build the list of results (make sure to pass pointers!).
2017-11-17 06:41:48 +00:00
results := []tb.InlineQueryResult{article}
2016-11-10 19:31:32 +00:00
// Build a response object to answer the query.
2017-11-17 06:41:48 +00:00
response := tb.QueryResponse{
2016-11-10 19:31:32 +00:00
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:
2016-11-10 19:31:32 +00:00
2015-07-03 16:56:35 +00:00
```go
2017-11-17 07:22:16 +00:00
f, err := tb.NewFile("boom.ogg")
2015-07-03 16:56:35 +00:00
if err != nil {
2016-11-10 19:31:32 +00:00
return err
2015-07-03 16:56:35 +00:00
}
2017-11-17 07:22:16 +00:00
audio := &tb.Audio{File: f}
2017-11-17 07:22:16 +00:00
// 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
```go
// Send a selective force reply message.
2017-11-17 13:55:18 +00:00
bot.Send(user, "pong", &tb.ReplyMarkup{
ForceReply: true,
Selective: true,
ReplyKeyboard: keys,
})
2015-07-03 16:56:35 +00:00
```