mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-05 22:43:59 +00:00
Initial release of go-telegram
A fully-generated, strongly-typed Go client for the Telegram Bot API. * 176 methods + 301 types generated from Bot API v10.0 * 1408 auto-generated tests (8 scenarios per method) * Typed unions throughout — no 'any' in the public surface * Pluggable HTTP transport and JSON codec (default goccy/go-json) * Built-in retry middleware honouring Telegram's retry_after * Generic dispatcher with filters and conversation handlers * Self-verifying codegen pipeline (regen → audit → emit → run tests) * 14 example bots covering common patterns
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
# callback
|
||||
|
||||
Inline keyboard with a counter that updates via callback queries. Demonstrates `OnCallback`, `AnswerCallbackQuery`, and `EditMessageText`.
|
||||
|
||||
## Run
|
||||
|
||||
```bash
|
||||
export TELEGRAM_BOT_TOKEN=...
|
||||
go run ./examples/callback
|
||||
```
|
||||
|
||||
Send `/start` in a chat with the bot.
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/api"
|
||||
"github.com/lukaszraczylo/go-telegram/client"
|
||||
"github.com/lukaszraczylo/go-telegram/dispatch"
|
||||
)
|
||||
|
||||
// register wires all handlers onto the router.
|
||||
func register(r *dispatch.Router) {
|
||||
r.OnCommand("/start", handleStart)
|
||||
r.OnCallback(`^count:(-?\d+):(inc|dec)$`, handleCallback)
|
||||
}
|
||||
|
||||
func handleStart(c *dispatch.Context, m *api.Message) error {
|
||||
return sendMenu(c.Ctx, c.Bot, m.Chat.ID, 0)
|
||||
}
|
||||
|
||||
func handleCallback(c *dispatch.Context, q *api.CallbackQuery) error {
|
||||
groups := c.Values["regex_match"].([]string)
|
||||
current, _ := strconv.Atoi(groups[1])
|
||||
if groups[2] == "inc" {
|
||||
current++
|
||||
} else {
|
||||
current--
|
||||
}
|
||||
|
||||
// Acknowledge the callback (removes the loading spinner).
|
||||
_, _ = api.AnswerCallbackQuery(c.Ctx, c.Bot, &api.AnswerCallbackQueryParams{
|
||||
CallbackQueryID: q.ID,
|
||||
Text: fmt.Sprintf("counter is now %d", current),
|
||||
})
|
||||
|
||||
// Edit the message to reflect the new state.
|
||||
if q.Message == nil {
|
||||
return nil
|
||||
}
|
||||
msg, ok := q.Message.(*api.Message)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
chatID := api.ChatIDFromInt(msg.Chat.ID)
|
||||
mid := msg.MessageID
|
||||
_, err := api.EditMessageText(c.Ctx, c.Bot, &api.EditMessageTextParams{
|
||||
ChatID: &chatID,
|
||||
MessageID: &mid,
|
||||
Text: fmt.Sprintf("Counter: %d", current),
|
||||
ReplyMarkup: counterKeyboard(current),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func sendMenu(ctx context.Context, bot *client.Bot, chatID int64, value int) error {
|
||||
_, err := api.SendMessage(ctx, bot, &api.SendMessageParams{
|
||||
ChatID: api.ChatIDFromInt(chatID),
|
||||
Text: fmt.Sprintf("Counter: %d", value),
|
||||
ReplyMarkup: counterKeyboard(value),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func counterKeyboard(value int) *api.InlineKeyboardMarkup {
|
||||
return &api.InlineKeyboardMarkup{
|
||||
InlineKeyboard: [][]api.InlineKeyboardButton{
|
||||
{
|
||||
{Text: "−", CallbackData: fmt.Sprintf("count:%d:dec", value)},
|
||||
{Text: "+", CallbackData: fmt.Sprintf("count:%d:inc", value)},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/api"
|
||||
"github.com/lukaszraczylo/go-telegram/client"
|
||||
"github.com/lukaszraczylo/go-telegram/dispatch"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type mockDoer struct{ mock.Mock }
|
||||
|
||||
func (m *mockDoer) Do(r *http.Request) (*http.Response, error) {
|
||||
args := m.Called(r)
|
||||
if v := args.Get(0); v != nil {
|
||||
return v.(*http.Response), args.Error(1)
|
||||
}
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
|
||||
func okResp(body string) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(bytes.NewBufferString(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
sendMsgResult = `{"ok":true,"result":{"message_id":1,"date":0,"chat":{"id":42,"type":"private"}}}`
|
||||
editMsgResult = `{"ok":true,"result":{"message_id":10,"date":0,"chat":{"id":42,"type":"private"}}}`
|
||||
answerCbResult = `{"ok":true,"result":true}`
|
||||
)
|
||||
|
||||
func makeCtx(bot *client.Bot, upd *api.Update, extra map[string]any) *dispatch.Context {
|
||||
c := dispatch.NewContext(context.Background(), bot, upd)
|
||||
for k, v := range extra {
|
||||
c.Values[k] = v
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// --- handleStart ---
|
||||
|
||||
func TestHandleStart_SendsInitialKeyboard(t *testing.T) {
|
||||
m := &mockDoer{}
|
||||
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
|
||||
if !strings.HasSuffix(r.URL.Path, "/sendMessage") {
|
||||
return false
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(r.Body)
|
||||
body := buf.String()
|
||||
// Counter starts at 0; keyboard must contain both buttons
|
||||
return strings.Contains(body, `"Counter: 0"`) &&
|
||||
strings.Contains(body, `"reply_markup"`) &&
|
||||
strings.Contains(body, `"count:0:dec"`) &&
|
||||
strings.Contains(body, `"count:0:inc"`)
|
||||
})).Return(okResp(sendMsgResult), nil)
|
||||
|
||||
bot := client.New("test:token", client.WithHTTPClient(m))
|
||||
msg := &api.Message{
|
||||
MessageID: 1,
|
||||
Chat: api.Chat{ID: 42, Type: string(api.ChatTypePrivate)},
|
||||
From: &api.User{ID: 7, FirstName: "Alice"},
|
||||
Text: "/start",
|
||||
}
|
||||
upd := &api.Update{UpdateID: 1, Message: msg}
|
||||
|
||||
require.NoError(t, handleStart(makeCtx(bot, upd, nil), msg))
|
||||
m.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// --- handleCallback ---
|
||||
|
||||
func callbackCtx(bot *client.Bot, q *api.CallbackQuery, groups []string) *dispatch.Context {
|
||||
upd := &api.Update{UpdateID: 1, CallbackQuery: q}
|
||||
return makeCtx(bot, upd, map[string]any{"regex_match": groups})
|
||||
}
|
||||
|
||||
func callbackQuery(data string, msgID int64, chatID int64) *api.CallbackQuery {
|
||||
msg := &api.Message{
|
||||
MessageID: msgID,
|
||||
Chat: api.Chat{ID: chatID, Type: string(api.ChatTypePrivate)},
|
||||
}
|
||||
return &api.CallbackQuery{
|
||||
ID: "cb1",
|
||||
From: api.User{ID: 7},
|
||||
Message: msg,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleCallback_Increments(t *testing.T) {
|
||||
m := &mockDoer{}
|
||||
// AnswerCallbackQuery
|
||||
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
|
||||
return strings.HasSuffix(r.URL.Path, "/answerCallbackQuery")
|
||||
})).Return(okResp(answerCbResult), nil)
|
||||
// EditMessageText — counter must show 6
|
||||
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
|
||||
if !strings.HasSuffix(r.URL.Path, "/editMessageText") {
|
||||
return false
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(r.Body)
|
||||
return strings.Contains(buf.String(), `"Counter: 6"`)
|
||||
})).Return(okResp(editMsgResult), nil)
|
||||
|
||||
bot := client.New("test:token", client.WithHTTPClient(m))
|
||||
q := callbackQuery("count:5:inc", 10, 42)
|
||||
// groups: [full_match, "5", "inc"]
|
||||
c := callbackCtx(bot, q, []string{"count:5:inc", "5", "inc"})
|
||||
|
||||
require.NoError(t, handleCallback(c, q))
|
||||
m.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestHandleCallback_Decrements(t *testing.T) {
|
||||
m := &mockDoer{}
|
||||
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
|
||||
return strings.HasSuffix(r.URL.Path, "/answerCallbackQuery")
|
||||
})).Return(okResp(answerCbResult), nil)
|
||||
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
|
||||
if !strings.HasSuffix(r.URL.Path, "/editMessageText") {
|
||||
return false
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(r.Body)
|
||||
return strings.Contains(buf.String(), `"Counter: 4"`)
|
||||
})).Return(okResp(editMsgResult), nil)
|
||||
|
||||
bot := client.New("test:token", client.WithHTTPClient(m))
|
||||
q := callbackQuery("count:5:dec", 10, 42)
|
||||
c := callbackCtx(bot, q, []string{"count:5:dec", "5", "dec"})
|
||||
|
||||
require.NoError(t, handleCallback(c, q))
|
||||
m.AssertExpectations(t)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package main demonstrates inline keyboards and callback queries with
|
||||
// go-telegram. Send /start to the bot in any chat to see two buttons.
|
||||
//
|
||||
// TELEGRAM_BOT_TOKEN=xxx go run ./examples/callback
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/client"
|
||||
"github.com/lukaszraczylo/go-telegram/dispatch"
|
||||
"github.com/lukaszraczylo/go-telegram/transport"
|
||||
)
|
||||
|
||||
func main() {
|
||||
token := os.Getenv("TELEGRAM_BOT_TOKEN")
|
||||
if token == "" {
|
||||
log.Fatal("TELEGRAM_BOT_TOKEN required")
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
bot := client.New(token,
|
||||
client.WithHTTPClient(client.NewRetryDoer(client.NewDefaultHTTPDoer())),
|
||||
)
|
||||
|
||||
router := dispatch.New(bot)
|
||||
register(router)
|
||||
|
||||
poller := transport.NewLongPoller(bot)
|
||||
if err := router.Run(ctx, poller); err != nil && err != context.Canceled {
|
||||
log.Printf("router exited: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user