Supported go version: 1.5, 1.6, tip
Implementation of the telegram bot API, inspired by github.com/go-telegram-bot-api/telegram-bot-api.
The main difference between telegram-bot-api and this version is supporting net/context. Also, this library handles errors more correctly at this time (telegram-bot-api v4).
- Client for telegram bot api.
- Bot with:
- Middleware support
- Command middleware to handle commands.
- Recover middleware to recover on panics.
- Webhook support
- Middleware support
Get last telegram api:
go get github.com/bot-api/telegram
go run ./examples/api/main.go -debug -token BOT_TOKEN
package main
import (
"log"
"flag"
"github.com/bot-api/telegram"
"golang.org/x/net/context"
)
func main() {
token := flag.String("token", "", "telegram bot token")
debug := flag.Bool("debug", false, "show debug information")
flag.Parse()
if *token == "" {
log.Fatal("token flag required")
}
api := telegram.New(*token)
api.Debug(*debug)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if user, err := api.GetMe(ctx); err != nil {
log.Panic(err)
} else {
log.Printf("bot info: %#v", user)
}
updatesCh := make(chan telegram.Update)
go telegram.GetUpdates(ctx, api, telegram.UpdateCfg{
Timeout: 10, // Timeout in seconds for long polling.
Offset: 0, // Start with the oldest update
}, updatesCh)
for update := range updatesCh {
log.Printf("got update from %s", update.Message.From.Username)
if update.Message == nil {
continue
}
msg := telegram.CloneMessage(update.Message, nil)
// echo with the same message
if _, err := api.Send(ctx, msg); err != nil {
log.Printf("send error: %v", err)
}
}
}
go run ./examples/echo/main.go -debug -token BOT_TOKEN
package main
// Simple echo bot, that responses with the same message
import (
"flag"
"log"
"github.com/bot-api/telegram"
"github.com/bot-api/telegram/telebot"
"golang.org/x/net/context"
)
func main() {
token := flag.String("token", "", "telegram bot token")
debug := flag.Bool("debug", false, "show debug information")
flag.Parse()
if *token == "" {
log.Fatal("token flag is required")
}
api := telegram.New(*token)
api.Debug(*debug)
bot := telebot.NewWithAPI(api)
bot.Use(telebot.Recover()) // recover if handler panic
netCtx, cancel := context.WithCancel(context.Background())
defer cancel()
bot.HandleFunc(func(ctx context.Context) error {
update := telebot.GetUpdate(ctx) // take update from context
if update.Message == nil {
return nil
}
api := telebot.GetAPI(ctx) // take api from context
msg := telegram.CloneMessage(update.Message, nil)
_, err := api.Send(ctx, msg)
return err
})
// Use command middleware, that helps to work with commands
bot.Use(telebot.Commands(map[string]telebot.Commander{
"start": telebot.CommandFunc(
func(ctx context.Context, arg string) error {
api := telebot.GetAPI(ctx)
update := telebot.GetUpdate(ctx)
_, err := api.SendMessage(ctx,
telegram.NewMessagef(update.Chat().ID,
"received start with arg %s", arg,
))
return err
}),
}))
err := bot.Serve(netCtx)
if err != nil {
log.Fatal(err)
}
}
Use callback query and edit bot's message
go run ./examples/callback/main.go -debug -token BOT_TOKEN
bot.HandleFunc(func(ctx context.Context) error {
update := telebot.GetUpdate(ctx) // take update from context
api := telebot.GetAPI(ctx) // take api from context
if update.CallbackQuery != nil {
data := update.CallbackQuery.Data
if strings.HasPrefix(data, "sex:") {
cfg := telegram.NewEditMessageText(
update.Chat().ID,
update.CallbackQuery.Message.MessageID,
fmt.Sprintf("You sex: %s", data[4:]),
)
api.AnswerCallbackQuery(
ctx,
telegram.NewAnswerCallback(
update.CallbackQuery.ID,
"Your configs changed",
),
)
_, err := api.EditMessageText(ctx, cfg)
return err
}
}
msg := telegram.NewMessage(update.Chat().ID,
"Your sex:")
msg.ReplyMarkup = telegram.InlineKeyboardMarkup{
InlineKeyboard: telegram.NewVInlineKeyboard(
"sex:",
[]string{"Female", "Male",},
[]string{"female", "male",},
),
}
_, err := api.SendMessage(ctx, msg)
return err
})
Take a look at ./examples/
to know more how to use bot and telegram api.
-
Handlers
-
Middleware
-
Command middleware
-
Session middleware
-
Log middleware
-
Menu middleware
-
Examples
- Command
- CallbackAnswer
- Inline
- Proxy
- Menu
-
Add travis-ci integration
-
Add coverage badge
-
Add integration tests
-
Add gopkg version
-
Improve documentation
-
Benchmark ffjson and easyjson.
-
Add GAE example.
- getMe
- sendMessage
- forwardMessage
- sendPhoto
- sendAudio
- sendDocument
- sendSticker
- sendVideo
- sendVoice
- sendLocation
- sendChatAction
- getUserProfilePhotos
- getUpdates
- setWebhook
- getFile
- answerInlineQuery inline bots
- sendVenue
- sendContact
- editMessageText
- editMessageCaption
- editMessageReplyMarkup
- kickChatMember
- unbanChatMember
- answerCallbackQuery
- getChat
- getChatMember
- getChatMembersCount
- getChatAdministrators
- leaveChat
- InlineQueryResultArticle
- InlineQueryResultAudio
- InlineQueryResultContact
- InlineQueryResultDocument
- InlineQueryResultGif
- InlineQueryResultLocation
- InlineQueryResultMpeg4Gif
- InlineQueryResultPhoto
- InlineQueryResultVenue
- InlineQueryResultVideo
- InlineQueryResultVoice
- InlineQueryResultCachedAudio
- InlineQueryResultCachedDocument
- InlineQueryResultCachedGif
- InlineQueryResultCachedMpeg4Gif
- InlineQueryResultCachedPhoto
- InlineQueryResultCachedSticker
- InlineQueryResultCachedVideo
- InlineQueryResultCachedVoice
- InputTextMessageContent
- InputLocationMessageContent
Other bots: I like this handler system https://bitbucket.org/master_groosha/telegram-proxy-bot/src/07a6b57372603acae7bdb78f771be132d063b899/proxy_bot.py?fileviewer=file-view-default