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

Feature Request: Telegram TEST mode #1039

Open
script-sorcerer opened this issue Apr 4, 2024 · 3 comments
Open

Feature Request: Telegram TEST mode #1039

script-sorcerer opened this issue Apr 4, 2024 · 3 comments
Assignees
Labels
K-feature-request Kind: request for implementing a feature

Comments

@script-sorcerer
Copy link

I couldn't find any description anywhere on how to make the bot send requests to the Telegram test environment (if there is any mention of it somewhere, please let me know).

I suggest adding an option to the bot called "mode," which will have values Prod (by default) / Test.

As an example, this can be done as follows:

let bot = Bot::from_env().set_mode(teloxide_core::bot::Mode::Test);

Then pass the value of mode to the function for creating the URL request:

// teloxide_core::net

fn method_url(base: reqwest::Url, token: &str, method_name: &str, mode: bot::Mode) -> reqwest::Url {
    match mode {
        bot::Mode::Prod => base.join(&format!("/bot{token}/{method_name}")).expect("failed to format url"),
        bot::Mode::Test => base.join(&format!("/bot{token}/test/{method_name}")).expect("failed to format url")
    }
}

Pros

  • Ability to execute commands in the test environment

Cons

Unable to find

@script-sorcerer script-sorcerer added the K-feature-request Kind: request for implementing a feature label Apr 4, 2024
@WaffleLapkin
Copy link
Member

There is Bot::set_api_url, which allows you to change thr bot API server url. As a workaround you could setup a reverse proxy which would redirect https://proxy/bot{token}/{method_name} to https://api.telegram.org/bot{token}/test/{method_name}.

I'm not sure I like the "mode enum" solution. Should we maybe just allow you to forge the full url? i.e. store something like fn(&str, &str) -> Url in the bot?

@script-sorcerer
Copy link
Author

There is Bot::set_api_url, which allows you to change thr bot API server url.

If I understand correctly, api_url will replace base in the method for creating the URL and add /bot{token}/{method_name}, which should not allow me to insert /test after the bot token.

Here's an example:

fn main() {
    let token = "_token_";
    let method_name = "_method_";
    let api_url = url::Url::parse(&format!("https://api.telegram.org/bot{token}/test")).unwrap();
    
    println!("api_url = {}, full = {}", api_url, api_url.join(&format!("/bot{token}/{method_name}")).unwrap());
}

result

api_url = https://api.telegram.org/bot_token_/test, full = https://api.telegram.org/bot_token_/_method_

As a workaround you could setup a reverse proxy which would redirect https://proxy/bot{token}/{method_name} to https://api.telegram.org/bot{token}/test/{method_name}.

It seems too convoluted for such a task. I'll have to set up a separate proxy application that is only needed for testing purposes. I'll try to implement this approach for now.

Should we maybe just allow you to forge the full url? i.e. store something like fn(&str, &str) -> Url in the bot?

Regarding this, I think it wouldn't hurt to consider the possibility of working with the test environment in a more convenient way than using a separate proxy 🤓

@WaffleLapkin
Copy link
Member

To be clear: the proxy thing is a workaround, not a solution. I think it's reasonable to expect teloxide to provide a convenient way of calling into the test environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
K-feature-request Kind: request for implementing a feature
Projects
None yet
Development

No branches or pull requests

3 participants