
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
github.com/bot-api/telegram
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).
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
Add travis-ci integration
Add coverage badge
Add integration tests
Add gopkg version
Improve documentation
Benchmark ffjson and easyjson.
Add GAE example.
Other bots: I like this handler system https://bitbucket.org/master_groosha/telegram-proxy-bot/src/07a6b57372603acae7bdb78f771be132d063b899/proxy_bot.py?fileviewer=file-view-default
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.