GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — bot/examples/middleware/main.go at main · go-telegram/bot Expand file tree/ Copy pathmain.goMore file actions 93 lines (80 loc) · 2.35 KB / Copy pathmain.goFile metadata and controls93 lines (80 loc) · 2.35 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 package main import ( "context" "log" "os" "os/signal" "sync" "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.WithMiddlewares(showMessageWithUserID, showMessageWithUserName), bot.WithDefaultHandler(handler), } 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.HandlerTypeCallbackQueryData, "", bot.MatchTypePrefix, func(ctx context.Context, b *bot.Bot, update *models.Update) { log.Printf("callback query data: %s", update.CallbackQuery.Data) }, singleFlight) b.Start(ctx) } func showMessageWithUserID(next bot.HandlerFunc) bot.HandlerFunc { return func(ctx context.Context, b *bot.Bot, update *models.Update) { if update.Message != nil { log.Printf("%d say: %s", update.Message.From.ID, update.Message.Text) } next(ctx, b, update) } } func showMessageWithUserName(next bot.HandlerFunc) bot.HandlerFunc { return func(ctx context.Context, b *bot.Bot, update *models.Update) { if update.Message != nil { log.Printf("%s say: %s", update.Message.From.FirstName, update.Message.Text) } next(ctx, b, update) } } type callbackFlightKey struct { chatID int64 messageID int } // singleFlight is a middleware that ensures that only one callback query is processed at a time. func singleFlight(next bot.HandlerFunc) bot.HandlerFunc { sf := sync.Map{} return func(ctx context.Context, b *bot.Bot, update *models.Update) { if update.CallbackQuery == nil { return } q := update.CallbackQuery var key any switch { case q.InlineMessageID != "": key = q.InlineMessageID case q.Message.Message != nil: key = callbackFlightKey{chatID: q.Message.Message.Chat.ID, messageID: q.Message.Message.ID} default: next(ctx, b, update) return } if _, loaded := sf.LoadOrStore(key, struct{}{}); loaded { return } defer sf.Delete(key) next(ctx, b, update) } } func handler(ctx context.Context, b *bot.Bot, update *models.Update) { b.SendMessage(ctx, &bot.SendMessageParams{ ChatID: update.Message.Chat.ID, Text: update.Message.Text, }) } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |