Skip to content

Commit

Permalink
upgrade to v7.2 (#150)
Browse files Browse the repository at this point in the history
* upgrade to v7.2

* fix parse mod

* all date fields now is int64

* feat: add date time getters

* feat: bussines account router support

* feat: return business in `Update.Msg()`

* chore: ignore types_gen.go in coverage

* chore: business bot sample and docs update

* docs: add emoji to features
  • Loading branch information
mr-linch authored Apr 29, 2024
1 parent a90a97b commit 39cc533
Show file tree
Hide file tree
Showing 12 changed files with 879 additions and 125 deletions.
1 change: 1 addition & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ignore:
- methods_gen.go
- types_gen.go
- examples
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/mr-linch/go-tg.svg)](https://pkg.go.dev/github.com/mr-linch/go-tg)
[![go.mod](https://img.shields.io/github/go-mod/go-version/mr-linch/go-tg)](go.mod)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/mr-linch/go-tg?label=latest%20release)](https://github.com/mr-linch/go-tg/releases/latest)
![Telegram Bot API](https://img.shields.io/badge/Telegram%20Bot%20API-7.1-blue?logo=telegram)
![Telegram Bot API](https://img.shields.io/badge/Telegram%20Bot%20API-7.2-blue?logo=telegram)
[![CI](https://github.com/mr-linch/go-tg/actions/workflows/ci.yml/badge.svg)](https://github.com/mr-linch/go-tg/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/mr-linch/go-tg/branch/main/graph/badge.svg?token=9EI5CEIYXL)](https://codecov.io/gh/mr-linch/go-tg)
[![Go Report Card](https://goreportcard.com/badge/github.com/mr-linch/go-tg)](https://goreportcard.com/report/github.com/mr-linch/go-tg)
Expand All @@ -29,9 +29,9 @@
- [Receive updates via Webhook](#receive-updates-via-webhook)
- [Routing updates](#routing-updates)
- [Extensions](#extensions)
- [Sessions](#sessions)
- [Sessions](#sessions)
- [Related Projects](#related-projects)
- [Projects using this package]
- [Projects using this package](#projects-using-this-package)
- [Thanks](#thanks)

go-tg is a Go client library for accessing [Telegram Bot API](https://core.telegram.org/bots/api), with batteries for building complex bots included.
Expand All @@ -40,14 +40,16 @@ go-tg is a Go client library for accessing [Telegram Bot API](https://core.teleg
## Features

- Code for Bot API types and methods is generated with embedded official documentation.
- Support [context.Context](https://pkg.go.dev/context).
- API Client and bot framework are strictly separated, you can use them independently.
- No runtime reflection overhead.
- Supports Webhook and Polling natively;
- [Webhook reply](https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates) for high load bots;
- Handlers, filters, and middleware are supported.
- [WebApps](https://core.telegram.org/bots/webapps) and [Login Widget](https://core.telegram.org/widgets/login) helpers.
- :rocket: Code for Bot API types and methods is generated with embedded official documentation.
- :white_check_mark: Support [context.Context](https://pkg.go.dev/context).
- :link: API Client and bot framework are strictly separated, you can use them independently.
- :zap: No runtime reflection overhead.
- :arrows_counterclockwise: Supports Webhook and Polling natively;
- :mailbox_with_mail: [Webhook reply](https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates) for high load bots;
- :raised_hands: Handlers, filters, and middleware are supported.
- :globe_with_meridians: [WebApps](https://core.telegram.org/bots/webapps) and [Login Widget](https://core.telegram.org/widgets/login) helpers.
- :handshake: Business connections support


## Install

Expand Down Expand Up @@ -375,18 +377,19 @@ client := tg.New("<TOKEN>",
)
```


Arguments of the interceptor are:
- `ctx` - context of the request;
- `req` - request object [tg.Request](https://pkg.go.dev/github.com/mr-linch/go-tg#Request);
- `dst` - pointer to destination for the response, can be `nil` if the request is made with `DoVoid` method;
- `invoker` - function for calling the next interceptor or the actual request.

- `ctx` - context of the request;
- `req` - request object [tg.Request](https://pkg.go.dev/github.com/mr-linch/go-tg#Request);
- `dst` - pointer to destination for the response, can be `nil` if the request is made with `DoVoid` method;
- `invoker` - function for calling the next interceptor or the actual request.

Contrib package has some useful interceptors:
- [InterceptorRetryFloodError](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorRetryFloodError) - retry request if the server returns a flood error. Parameters can be customized via options;
- [InterceptorRetryInternalServerError](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorRetryInternalServerError) - retry request if the server returns an error. Parameters can be customized via options;
- [InterceptorMethodFilter](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorMethodFilter) - call underlying interceptor only for specified methods;
- [InterceptorDefaultParseMethod](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorDefaultParseMethod) - set default `parse_mode` for messages if not specified.

- [InterceptorRetryFloodError](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorRetryFloodError) - retry request if the server returns a flood error. Parameters can be customized via options;
- [InterceptorRetryInternalServerError](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorRetryInternalServerError) - retry request if the server returns an error. Parameters can be customized via options;
- [InterceptorMethodFilter](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorMethodFilter) - call underlying interceptor only for specified methods;
- [InterceptorDefaultParseMethod](https://pkg.go.dev/github.com/mr-linch/go-tg#NewInterceptorDefaultParseMethod) - set default `parse_mode` for messages if not specified.

Interceptors are called in the order they are registered.

Expand Down Expand Up @@ -451,7 +454,7 @@ These handlers will only be called for updates of a certain type, the rest will

List of typed handlers:

- [`tgb.MessageHandler`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#MessageHandler) with [`tgb.MessageUpdate`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#MessageUpdate) for `message`, `edited_message`, `channel_post`, `edited_channel_post`;
- [`tgb.MessageHandler`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#MessageHandler) with [`tgb.MessageUpdate`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#MessageUpdate) for `message`, `edited_message`, `channel_post`, `edited_channel_post`, `business_message`, `edited_business_message`;
- [`tgb.InlineQueryHandler`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#InlineQueryHandler) with [`tgb.InlineQueryUpdate`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#InlineQueryUpdate) for `inline_query`
- [`tgb.ChosenInlineResult`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#ChosenInlineResult) with [`tgb.ChosenInlineResultUpdate`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#ChosenInlineResultUpdate) for `chosen_inline_result`;
- [`tgb.CallbackQueryHandler`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#CallbackQueryHandler) with [`tgb.CallbackQueryUpdate`](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb#CallbackQueryUpdate) for `callback_query`;
Expand Down Expand Up @@ -752,7 +755,8 @@ See [session](https://pkg.go.dev/github.com/mr-linch/go-tg/tgb/session) package
- [`bots-house/docker-telegram-bot-api`](https://github.com/bots-house/docker-telegram-bot-api) - docker image for running self-hosted Telegram Bot API with automated CI build;

## Projects using this package
- [@ttkeeperbot](https://t.me/ttkeeperbot) - Automatically upload tiktoks in groups and verify users 🇺🇦

- [@ttkeeperbot](https://t.me/ttkeeperbot) - Automatically upload tiktoks in groups and verify users 🇺🇦

## Thanks

Expand Down
5 changes: 3 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Listen port set to `:8000` by default, you can override it with the `-webhook-li
| [text-filter](https://github.com/mr-linch/go-tg/tree/main/examples/text-filter) | Text Filter usage | Text filter, reply keyboard markup |
| [webapps](https://github.com/mr-linch/go-tg/tree/main/examples/webapps) | Parse and validate Login Widget & WebApp data, host simple webapp | WebApps, Login Widget, Embed webhook to http.Mux |
| [session-filter](https://github.com/mr-linch/go-tg/tree/main/examples/session-filter) | Simple form filling with persistent session | Router, Session Manager, Session Filters |
| [menu](https://github.com/mr-linch/go-tg/tree/main/examples/menu) | Hiearchical menu with API integration | ButtonLayout, TextMessageBuilder, CallbackDataFilter |
| [retry-flood](https://github.com/mr-linch/go-tg/tree/main/examples/retry-flood) | Retry on flood error | Interceptors |
| [menu](https://github.com/mr-linch/go-tg/tree/main/examples/menu) | Hiearchical menu with API integration | ButtonLayout, TextMessageBuilder, CallbackDataFilter |
| [retry-flood](https://github.com/mr-linch/go-tg/tree/main/examples/retry-flood) | Retry on flood error | Interceptors |
| [business-bot](https://github.com/mr-linch/go-tg/tree/main/examples/business-bot) | Business bot with multiple handlers | Router |
79 changes: 79 additions & 0 deletions examples/business-bot/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Package contains simple echo bot, that demonstrates how to use handlers, filters and file uploads.
package main

import (
"context"
"fmt"
"log"
"strings"

"github.com/mr-linch/go-tg"
"github.com/mr-linch/go-tg/examples"
"github.com/mr-linch/go-tg/tgb"
)

var pm = tg.HTML

func main() {
examples.Run(tgb.NewRouter().
Message(func(ctx context.Context, mu *tgb.MessageUpdate) error {
me, err := mu.Client.GetMe().Do(ctx)
if err != nil {
return fmt.Errorf("get me: %w", err)
}

if !me.CanConnectToBusiness {
return mu.Answer("Bussines features is not enabled for current bot. Enable it via @BotFather").DoVoid(ctx)
}

if mu.From != nil && !mu.From.IsPremium {
return mu.Answer("Bussines features works only for Telegram Premium users. Purchase subscription before use that bot.").DoVoid(ctx)
}

return mu.Answer("Connect bot in Telegram Bussines settings of your account").DoVoid(ctx)
}, tgb.Command("start")).
BusinessMessage(func(ctx context.Context, mu *tgb.MessageUpdate) error {
if strings.Contains(mu.Text, "ping") {
return mu.Answer("Pong!").BusinessConnectionID(mu.BusinessConnectionID).DoVoid(ctx)
}

log.Printf("New business message #%d: %s", mu.ID, mu.Text)

return nil
}).
EditedBusinessMessage(func(ctx context.Context, mu *tgb.MessageUpdate) error {
log.Printf("Edited business message: %#v", mu.Message)

return nil
}).
BusinessConnection(func(ctx context.Context, bcu *tgb.BusinessConnectionUpdate) error {
log.Printf("New business connection: %#v", bcu.BusinessConnection)

lines := []string{}

if bcu.IsEnabled {
lines = append(lines, "🤝 Business connection estabilished")
} else {
lines = append(lines, "❌ Business connection closed")
}

lines = append(lines, "")

lines = append(lines, pm.Line(
pm.Bold("ID: "), pm.Code(bcu.BusinessConnection.ID),
))

lines = append(lines, pm.Line(
pm.Bold("Can Reply? "), pm.Code(fmt.Sprintf("%t", bcu.BusinessConnection.CanReply)),
))

return bcu.Update.Reply(ctx,
tg.NewSendMessageCall(
bcu.User,
pm.Text(lines...),
).
ParseMode(pm),
)
}),
)
}
Loading

0 comments on commit 39cc533

Please sign in to comment.