Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export more types #1659

Closed
AlexXanderGrib opened this issue Jul 18, 2022 · 3 comments
Closed

Export more types #1659

AlexXanderGrib opened this issue Jul 18, 2022 · 3 comments

Comments

@AlexXanderGrib
Copy link

Is your feature request related to a problem? Please describe.
For my bot i decided to create custom context so it can be convinient to send messages and interact with db. And i wanted to write custom .reply() method that detects what file i need to send. And also i want it to be type-safe

Describe the solution you'd like

import type { ExtraReplyMessage } from "telegraf";

Describe alternatives you've considered

export class CustomContext extends Context {
  async send(
    text: string,
    {
      file,
      ...rest
    }: Parameters<typeof this.replyWithPhoto>[1] & { file?: string } = {}
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //              This crap
  ): Promise<Message> {
    const options = {
      reply_to_message_id: this.message?.message_id,
      allow_sending_without_reply: true,
      parse_mode: "HTML" as const,
      caption: text,
      ...rest
    };

    const match = createMatcher<Promise<Message>, [typeof options]>([
      [
        /\.(jpe?g|png|svg|webp)$/,
        (url, options) => this.replyWithPhoto({ url }, options)
      ],
      [/\.(mp3|ogg)$/, (url, options) => this.replyWithAudio({ url }, options)],
      [
        /\.(apng|gif)$/,
        (url, options) => this.replyWithAnimation({ url }, options)
      ],
      [
        /\.(mp4|webm|av1)$/,
        (url, options) => this.replyWithVideo({ url }, options)
      ],
      [/\.opus$/, (url, options) => this.replyWithVoice({ url }, options)],
      [/.*/, (_file, options) => this.replyWithHTML(options.caption, options)]
    ]);

    return await match(file ?? "", options);
  }
}

Also this issue effects middlewares

type Middleware = Parameters<typeof bot["command"]>[1];
export type MessageContext = Parameters<
  Middleware extends MiddlewareFn<infer X> | MiddlewareObj<infer X>
    ? MiddlewareFn<X>
    : never
>[0];
@MKRhere
Copy link
Member

MKRhere commented Jul 25, 2022

Taken into consideration, will try to add these exports in the next minor release. However, Extra* types are a bit sketch (you'd still need the non-extra params to get full information about a method), so I'll think a little more into how it could be better exposed. If you have ideas on those lines, do let me know.

@MKRhere
Copy link
Member

MKRhere commented Aug 24, 2022

Exposed in 9548a21 in the experimental types export, will land in 4.9.0. Watch #1671

Usage:

import type { Convenience } from "telegraf/types";

// Convenience.ExtraReplyMessage

@MKRhere
Copy link
Member

MKRhere commented Aug 26, 2022

Completed in #1671, v4.9.0

@MKRhere MKRhere closed this as completed Aug 26, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants