46992b037b
These changes make it possible to set custom options when responding to inline queries, as described on https://core.telegram.org/bots/api#answerinlinequery. It also includes all the (non-cached) inline result types as described at https://core.telegram.org/bots/api#inlinequeryresult. Some remarks: * The internals of sendCommand have changed. It now expects a JSON-serializable object. Instead of doing GET requests with URL-encoded query parameters it now POSTS JSON directly. * Because of the above, sendFile() has changed as well. It now expects a * `map[string]string` which it will internally convert to URL encoded form values. * Respond has been deprecated in favor of the new AnswerInlineQuery function. It is only kept for backward compatibility. * A dependency on https://github.com/mitchellh/hashstructure has been introduced in order to generate automatic IDs for inline results. |
8 years ago | |
---|---|---|
.gitignore | 9 years ago | |
.travis.yml | 9 years ago | |
LICENSE | 9 years ago | |
README.md | 8 years ago | |
api.go | 8 years ago | |
bot.go | 8 years ago | |
file.go | 9 years ago | |
inline.go | 8 years ago | |
inline_article.go | 8 years ago | |
inline_types.go | 8 years ago | |
input_types.go | 8 years ago | |
message.go | 8 years ago | |
options.go | 8 years ago | |
telebot.go | 9 years ago | |
telebot_test.go | 8 years ago | |
types.go | 8 years ago |
README.md
Telebot
Telebot is a convenient wrapper to Telegram Bots API, written in Golang.
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 care about networking at all. Here is an example "helloworld" bot, written with telebot:
import (
"time"
"github.com/tucnak/telebot"
)
func main() {
bot, err := telebot.NewBot("SECRET TOKEN")
if err != nil {
log.Fatalln(err)
}
messages := make(chan telebot.Message)
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. Telebot support inline mode in a fancy manner. Here's a nice way to handle both incoming messages and inline queries:
import (
"log"
"time"
"github.com/tucnak/telebot"
)
var bot *telebot.Bot
func main() {
bot, err := telebot.NewBot("SECRET TOKEN")
if err != nil {
log.Fatalln(err)
}
bot.Messages = make(chan telebot.Message, 1000)
bot.Queries = make(chan telebot.Query, 1000)
go messages()
go queries()
bot.Start(1 * time.Second)
}
func messages() {
for message := range bot.Messages {
// ...
}
}
func queries() {
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)
}
}
}
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", "#"},
},
},
},
)