|
| 1 | +// Command pp is a bot that pretty-prints the raw MTProto message behind any |
| 2 | +// Bot API message. Send it anything and it replies with the tdp.Format dump of |
| 3 | +// the underlying tg.Message, reachable through Message.Raw — useful for |
| 4 | +// inspecting fields the typed Bot API surface does not expose. |
| 5 | +// |
| 6 | +// Run it with an MTProto app identity (https://my.telegram.org) and a BotFather |
| 7 | +// token: |
| 8 | +// |
| 9 | +// APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/pp |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "os" |
| 15 | + "os/signal" |
| 16 | + "strconv" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/gotd/log/logzap" |
| 20 | + "github.com/gotd/td/tdp" |
| 21 | + "go.uber.org/zap" |
| 22 | + |
| 23 | + "github.com/gotd/botapi" |
| 24 | + "github.com/gotd/botapi/storage" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + log, _ := zap.NewProduction() |
| 29 | + defer func() { _ = log.Sync() }() |
| 30 | + |
| 31 | + appID, err := strconv.Atoi(os.Getenv("APP_ID")) |
| 32 | + if err != nil { |
| 33 | + log.Fatal("APP_ID must be a number (see https://my.telegram.org)", zap.Error(err)) |
| 34 | + } |
| 35 | + |
| 36 | + // Persist session, peers and update state so the bot resumes across restarts. |
| 37 | + store, err := storage.Open("session.bbolt") |
| 38 | + if err != nil { |
| 39 | + log.Fatal("Open storage", zap.Error(err)) |
| 40 | + } |
| 41 | + |
| 42 | + defer func() { _ = store.Close() }() |
| 43 | + |
| 44 | + bot, err := botapi.New(os.Getenv("BOT_TOKEN"), botapi.Options{ |
| 45 | + AppID: appID, |
| 46 | + AppHash: os.Getenv("APP_HASH"), |
| 47 | + Logger: logzap.New(log), |
| 48 | + Storage: store, |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + log.Fatal("Create bot", zap.Error(err)) |
| 52 | + } |
| 53 | + |
| 54 | + bot.Use(botapi.Recover(), botapi.Timeout(30*time.Second)) |
| 55 | + |
| 56 | + bot.OnCommand("start", "Show the welcome message", func(c *botapi.Context) error { |
| 57 | + _, err := c.Reply("Reply to any message with /pp to dump its raw MTProto message.") |
| 58 | + return err |
| 59 | + }) |
| 60 | + |
| 61 | + // /pp pretty-prints the MTProto message the command replies to. The replied |
| 62 | + // message is not carried by the update, so it is fetched with Bot.GetMessage, |
| 63 | + // whose raw tg.Message (via Message.Raw) is dumped with tdp.Format. |
| 64 | + bot.OnCommand("pp", "Pretty-print the replied-to MTProto message", func(c *botapi.Context) error { |
| 65 | + msg := c.Message() |
| 66 | + |
| 67 | + reply := msg.ReplyToMessage |
| 68 | + if reply == nil { |
| 69 | + _, err := c.Reply("Reply to a message with /pp to dump it.") |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + target, err := c.Bot.GetMessage(c, botapi.ID(msg.Chat.ID), reply.MessageID) |
| 74 | + if err != nil { |
| 75 | + _, replyErr := c.Reply("Could not fetch the replied message: " + err.Error()) |
| 76 | + if replyErr != nil { |
| 77 | + return replyErr |
| 78 | + } |
| 79 | + |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + _, err = c.Reply(tdp.Format(target.Raw())) |
| 84 | + |
| 85 | + return err |
| 86 | + }) |
| 87 | + |
| 88 | + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) |
| 89 | + defer cancel() |
| 90 | + |
| 91 | + log.Info("Starting pp bot") |
| 92 | + |
| 93 | + if err := bot.Run(ctx); err != nil { |
| 94 | + log.Fatal("Run", zap.Error(err)) |
| 95 | + } |
| 96 | +} |
0 commit comments