I am facing an issue with understanding the different message properties in Telegraf (4.6.0) types.
This is what I currently have:
import { Telegraf, Context } from 'telegraf'
const myBot = new Telegraf(myToken)
listenerBot.on('message', (ctx: Context) => {
const {
text,
forward_from_chat, forward_from_message_id,
photo, caption, caption_entities,
// unsure about properties for video, audio, document, etc.
} = ctx.message as any
// perform actions with the message
}
Due to messages being of various types, when I type ctx.message.
in my IDE (specifically VS Code), only suggested props that are always present in the message object like message_id
appear. Although I can check using conditions like
if('text' in ctx.message) {
// do something with ctx.message.text
}
this approach does not help me discover all possible props that ctx.message
may contain. One potential method could be
class ExploredContext = ExploreProps<Context> → provides a class resembling Context where
all potential props are non-optional
...
(ctx as ExploredContext).message._ // cursor here, IDE displays available props
(ctx.message as ExploredMessage)._ // or similar
However, I am uncertain how to create a helper like ExploreProps
since I am only familiar with utility types. Additionally, I am unaware of any cleaner alternatives to achieve this goal without resorting to hacky methods involving TypeScript configurations and/or IDE settings.
Can you recommend a technique for implementing ExploreProps
or a more efficient way to explore potential props?
(I also inquired about this in a Telegraf issue, but a universal solution would be beneficial regardless of Telegraf)