58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"github.com/go-telegram/bot"
|
|
"github.com/go-telegram/bot/models"
|
|
)
|
|
|
|
// Send any text message to the bot after the bot has been started
|
|
|
|
func main() {
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer cancel()
|
|
|
|
opts := []bot.Option{
|
|
bot.WithDefaultHandler(defaultHandler),
|
|
}
|
|
|
|
b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
|
|
if nil != err {
|
|
// panics for the sake of simplicity.
|
|
// you should handle this error properly in your code.
|
|
panic(err)
|
|
}
|
|
|
|
b.RegisterHandler(bot.HandlerTypeMessageText, "foo", bot.MatchTypeCommand, fooHandler)
|
|
b.RegisterHandler(bot.HandlerTypeMessageText, "bar", bot.MatchTypeCommandStartOnly, barHandler)
|
|
|
|
b.Start(ctx)
|
|
}
|
|
|
|
func fooHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
|
|
b.SendMessage(ctx, &bot.SendMessageParams{
|
|
ChatID: update.Message.Chat.ID,
|
|
Text: "Caught *foo*",
|
|
ParseMode: models.ParseModeMarkdown,
|
|
})
|
|
}
|
|
|
|
func barHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
|
|
b.SendMessage(ctx, &bot.SendMessageParams{
|
|
ChatID: update.Message.Chat.ID,
|
|
Text: "Caught *bar*",
|
|
ParseMode: models.ParseModeMarkdown,
|
|
})
|
|
}
|
|
|
|
func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
|
|
b.SendMessage(ctx, &bot.SendMessageParams{
|
|
ChatID: update.Message.Chat.ID,
|
|
Text: "Say message with `/foo` anywhere or with `/bar` at start of the message",
|
|
ParseMode: models.ParseModeMarkdown,
|
|
})
|
|
}
|