grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
2.33k stars 112 forks source link

ctx.forwardMessage 'optional' options object causing typescript error 2345 #270

Closed sartoshi-foot-dao closed 2 years ago

sartoshi-foot-dao commented 2 years ago

SOLVED: https://github.com/grammyjs/grammY/issues/270#issuecomment-1228709629

Reproduction

const targetChatId = 123;
                const fromChatId = 456;
                const message_id = 789

                ctx.forwardMessage(targetChatId, {
                    from_chat_id: fromChatId, // from_chat_id does not exist 'Other<"forwardMessage, "chat_id" | "from_chat_id" | "message_id">'
                    message_id
                })

Workaround

bot.api.forwardMessage(targetChatId, fromChatId, message_id); // QED.

Notes

  1. Using latest versions of all grammY packages.
  2. from_chat_id and message_id are required fields, even though I am passing them as an 'optional' object. I could be misusing it.
  3. ctx.reply has an almost identical type definition format, triggering autocomplete beautifully. Using the options object for ctx.forwardMessage however fails show anything.
sartoshi-foot-dao commented 2 years ago

Solution

ctx.forwardMessage, much like ctx.reply is a shortcut that is bound to the current message i.e. its from_chat_id and message_id are filled in for you. All that's required is the target chatId. Neat!

To define other values, use ctx.api.forwardMessage. Noice.

Thanks to @Loskir for the rapido response.

KnorpelSenf commented 2 years ago

Also to be found at https://grammy.dev/guide/context.html#available-actions

Do you think there's a better way to explain it, so that this becomes easier to understand?

dcdunkan commented 2 years ago

@KnorpelSenf I have a suggestion.

A little below under "Telegram Reply Feature", there is this text:

Naturally, every other method on ctx.api has a shortcut with the correct pre-filled values, such as ctx.replyWithPhoto to reply with a photo, or ctx.exportChatInviteLink to get an invite link for the respective chat.

My suggestion is to showcase a few more common methods such as forwardMessage, answerCallbackQuery, replyWithPhoto, exportChatInviteLink (because we're talking about it above) etc.

Even better, we could also do a ctx.api.forwardMessage vs. ctx.forwardMessage kinda thing for those methods as etc.

ctx.api.forwardMessage(
  chat_id,
  from_chat_id,
  message_id,
);

// shorter, from_chat_id and message_id are pre-filled:
ctx.forwardMessage(chat_id);

What do you think?

KnorpelSenf commented 2 years ago

Sure, that'd be simple to do.

Was this the reason you opened this issue? In other words, did you read the respective section but didn't see how to relate it to your case?

I'm thinking, if you didn't see the part of the docs at all, then the problem isn't that it's not clear enough, but rather that it's not easy enough to find. Hence, we should look into ways to increase the visibility, such as

sartoshi-foot-dao commented 2 years ago

@KnorpelSenf I had seen this section, albeit only during my initial exploration of grammY. Never been back there since, as ctx.reply was sufficient and intuitive for my then level. I spent a lot of time reading and carefully re-reading this section, as that was what gave me the original 'aha' moment for ctx. After reading the Context/AvailableActions section, I think I'd just assumed that chatId would be abstracted away in nearly all cases. Today's issue was that I didn't expect ctx to be that helpful i.e. taking care of from_chat_id and message_id too! I could only suggest a gentle teaser of ctx's power, similar to @dcdunkan 's suggestion of succinct comparisons, as this may have induced an even grander 'aha' and built more anticipation for the subsequent Context section.

With regards to clarity/findability - a slight boost in both would have alleviated my issues. Regarding findability, I had literally Cmd+K searched forwardMessage in the vain hope of finding an example; all it would have taken is one mention of a 'best practice' with ctx, or a cheatsheet of sorts, that would have solved my issue. Regarding clarity, a more explicit reason than 'alias' to use ctx, such as the proposition of saving time and writing cleaner code, would have also led me sooner to the possibility that I may have been overcomplicating things!

Once I had started using the plugins intensively and began getting creative with grammY's capabilities, revisiting the Context section simply didn't cross my mind; I thought I had understood ctx for the fantastically intuitive multitool that it was. However, when a roadblock was encountered, the instinct was to search directly for the 'more advanced' term forwardMessage than for the more basic one 'Context'. Perhaps ctx's conveniences are a bit too seamless?

The documentation is quite possibly the clearest and most proactively helpful I have read, so it really does take a short essay to express the nuanced context of the problem I faced today. I've enjoyed every minute of using grammY and look forward to contributing more, if only in the form of documenting solutions on GitHub Issues.

KnorpelSenf commented 2 years ago

Alright, then I think adding a few more examples is the right way to go. Do you feel like working on this?

I had literally Cmd+K searched forwardMessage in the vain hope of finding an example

We cannot add examples for all API methods, that's too much work to maintain. It would be awesome to be able to crawl https://doc.deno.land/https://deno.land/x/grammy/mod.ts and add the search results to the index, but I am afraid that Algolia can only crawl one page in the free OSS version. We may be able to do some manual work and add entries for the API to the index, but until then we provide https://grammy.dev/guide/#about-the-documentation which explains where to look.

Perhaps ctx's conveniences are a bit too seamless?

They will be reworked for 2.0 in the distant future, you are more than welcome to participate in this process!

sartoshi-foot-dao commented 2 years ago

I understand; thanks for the consideration. I'd be happy to give it a shot. Wednesday will have time. I'll keep watch in the issues and group chat for the rework.

KnorpelSenf commented 2 years ago

Great to hear!