Forums

Whitelist suggestion for telegram bot api

Hi i was trying to do some tests with the telegram bot api and i found that you have a whitelist for sites. In your requirements for a whitelisting a site it must offer a public API, so the telegram API seems to fit those requirements.

The urls for the API is : https://api.telegram.org

Thanks, and a very nice and smooth site BTW i like it very much, great work! :D

Hi markcial,

Thanks for your nice comments. I just added api.telegram.org to the whitelist. You should be able to access it now.

Conrad

great! thanks! :)

Hi markcial

have you ever created any telegram bot? I have done one and it's code is on PythonAnyWhere.com. I use webhook and the problem is that I dont get any response from telegram when sending command

there is any related log in server log. Can you please help?

Tony

Maybe we can help... What do you mean when you say you're using a webhook? And when you say you don't get a response from telegram?

Hi there, yup the code is hosted here => https://github.com/Markcial/CrapBot

It works great, the pythonanywhere service is really great.

Are you sure that you are using the webhooks properly? The webhooks need the full url, for example, https://username.pythonanywhere.com/endpoint

Cheers

I think I've set it correctly

https://api.telegram.org/botMY_TOKEN/setWebhook?url=https://tonyzad.pythonanywhere.com/bot/r1

but the problem is when I send command from telegram, I have print('req from teleg') in my function to know if the request was sent but noting is in sever log.

dear Harry

webHook is a callback mechanism when a telegram receives a command, Telegram sends POST request to given HTTPS URL.

I set the webhook and expect to receive the request and see the log in server log(I print something there) when send command to my bot. but the text and anything about access log is there whereas if I run the script, the log will be there

can you help?

Ah yes, we use webhooks for some of our 3rd party integrations too.

So you're sending requests to telegram, but you think they're not sending you the webhook callbacks?

I don't quite understand what you're saying here: "but the text and anything about access log is there whereas if I run the script, the log will be there"?

@markcial, webhooks are working OK for you?

in the callback function I have commented everything but a print("some thing"). If I see the text in sever log after I send command to telegram bot, I can be sure that telegram calls my function however there is no such text in my server log.

this is my code

def r1(request): print("**some text * ") return HttpResponse('hypBox Test.', status=200)

what do you think?

def r1(request):
    print("******some text * ")
    return HttpResponse('hypBox Test.', status=200)

by the way I am really enjoying every minute working with your service. A W E S O M E Thanks

Ah, I understand. Are you sure that just doing a print will end up in the server log? Try printing to stderr instead:

print("hello", file=sys.stderr)

(you may need to import sys )...

yes I can see the logs when I browse the page.

by the way, beside server_log, if telegram calls my page then I should see the history in access_log. right? but it is not there either.

I guess then it sounds like telegram isn't sending those webhooks... Is it in "test mode" or something? Maybe it only sends webhooks when you switch to "live mode"? Just an idea...

I don't know, I am totally confused and just don't know what to do.

what is self-signed ssl? Is your ssl self-signed, if so, I must upload the pem file too

what about ports

Ports currently supported for Webhooks: 443, 80, 88, 8443.

Don't know if it helps, but with flask the request already has a json object with the webhook data, like this:

@app.route('/bot', methods=['POST', 'GET'])
def bot():
    update = request.json
    if update:
        crap_bot.handle(update)
    return 'Ok'

your webhook URL will be using our SSL cert for *.pythonanywhere.com, which is not self-signed, so that's not the problem. It will work on port 80 (the http port) and port 443 (the https port). So that all sounds fine...

Ok. thank you Harry. I think I'd better contact telegram support. everything is ok here.

Thanks dud

Dear Harry

I fixed the problem, now telegram send me POST request but in the server log I can see that it receives 403 error, so telegram will send request again. their IP is 149.154.167.197 and my access log is https://www.pythonanywhere.com/user/tonyzad/files/var/log/tonyzad.pythonanywhere.com.access.log you can see the req and responses.

what do you think, is it firewall or something which 403 is the response. in my code I just returned 200

bests Tony

The 403 will be coming from your app I think. Flask usually gives a 403 if you try to access a form value that doesn't exist, or if you send, eg, a POST request to a view that only does GETs, or something like that...

Are you sure telegram sends a POST?

Could "request.json" be raising an exception if there is no request.json?

you are right, I just forgot about CSRF.

thank you harry, you really helped me about that.

:)

and one more thing.

I want to send photo with telegram bot. The thing is that when sending file with Telegram Bot will download the photo with urllib.urlretrieve and then send it but it can not download the photo on the server whereas I can run it correctly on my local machine.

does my code need any kind of permission to write in a file?

Thanks

Anybody HERE?

If you're not getting an error from urlretrieve, it probably is downloading the file, just not to where you expect. If you specify a full path to save the file to, you'll always know where it ends up.

this is the error.

<urlopen error Tunnel connection failed: 403 Forbidden>

how can I get rid of it? Could you please help?

Tony

I now understand about white-list. so it's ok

:)

Hi

It's me again. :) Could you please add http://www.tgju.org/ to white list? It is public website for stock and metal prices. It is not a critical or copyrighted content,

I have their api and can not access to it (403 error).

best Tony

I found that they have an API, but I can't read the language, so I couldn't find what the API endpoint was. Could you look that up and let me know?

It is a public api but not free. also the link is not in their site, they give it after purchasing the api.

thanks for your response Tony

OK. We still need to know what domain they serve the API from. Is it www.tgju.org, or is it api.tgju.org, or something else?

Thank you for your time. The api is on the www.tgju.org domain. Not api.tgju.org

Thanks

OK, give it a try now?

I hope you'll be purchasing our services as well, since you've apparently got the budget to pay for other online services ;-)

thanks for your help. About purchasing your service. Yes as soon as possible. we are going to do that.

thanks