https://github.com/alariczq/go-tgbot
Quickly create telegram bots.
https://github.com/alariczq/go-tgbot
go golang telegram telegram-bot telegram-bot-api
Last synced: 5 months ago
JSON representation
Quickly create telegram bots.
- Host: GitHub
- URL: https://github.com/alariczq/go-tgbot
- Owner: alariczq
- License: mit
- Created: 2022-05-12T18:15:46.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-05T10:24:06.000Z (almost 4 years ago)
- Last Synced: 2025-01-06T15:21:56.918Z (over 1 year ago)
- Topics: go, golang, telegram, telegram-bot, telegram-bot-api
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-tgbot
[](https://pkg.go.dev/github.com/imzhongqi/go-tgbot)
[](https://goreportcard.com/report/github.com/imzhongqi/go-tgbot)
[](https://opensource.org/licenses/MIT)
Wrapped [telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) to create telegram bot faster.
## 1. Installation
Run the following command under your project:
```
go get -u github.com/imzhongqi/go-tgbot
```
## 2. Example
```go
package main
import (
"log"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/imzhongqi/go-tgbot"
"github.com/panjf2000/ants/v2"
)
func main() {
api, err := tgbotapi.NewBotAPI("xxx")
if err != nil {
panic(err)
}
pool, err := ants.NewPool(10000, ants.WithExpiryDuration(10*time.Second))
if err != nil {
panic(err)
}
bot := tgbot.NewBot(api,
tgbot.WithTimeout(2*time.Second),
// tgbot.WithWorkersNum(-1), // use can use unlimited workers.
tgbot.WithWorkersPool(tgbot.NewAntsPool(pool)),
tgbot.WithUpdatesHandler(func(ctx *tgbot.Context) {
err := ctx.ReplyText(ctx.Message().Text, func(c *tgbotapi.MessageConfig) {
c.ReplyToMessageID = ctx.Message().MessageID
})
if err != nil {
log.Printf("reply text error: %s", err)
}
}),
tgbot.WithUndefinedCmdHandler(func(ctx *tgbot.Context) error {
return ctx.ReplyMarkdown("*unknown command*", tgbot.WithDisableWebPagePreview(false))
}),
tgbot.WithErrorHandler(func(err error) {
log.Println(err)
}),
)
bot.AddCommands(
tgbot.NewCommand("ping", "ping the bot", func(ctx *tgbot.Context) error {
return ctx.ReplyMarkdown("pong")
},
tgbot.WithHide(true),
tgbot.WithScopes(
tgbot.CommandScopeDefault(),
tgbot.CommandScopeAllGroupChats(),
tgbot.CommandScopeChat(100),
),
),
)
if err := bot.Run(); err != nil {
panic(err)
}
}
```