If you wanna use Telegram API on a server that can't access it, mainly because of being banned by Telegram or the country itself blocking Telegram from inside, like Iran, you can use Cloudflare workers as something like a proxy to send and receive requests from the API for free.
In the worker.js file you can find a simple script that gets the text from the url parameter and sends it to a Telegram user.
- Create a Bot in Telegram using the https://t.me/BotFather; You need the Bot's TOKEN, and the User's CHAT_ID.
- Create a free Cloudflare account.
- Create a worker in the Cloudflare dashboard, name it whatever you want, and paste the code from worker.js file into worker.js file in the cloudflare's worker.
- Change the TOKEN and CHAT_ID with your data, you can either put them directly in the script or use an environment variable.
- Deploy the worker and get the its url.
- Now you should be able to send a message to the user by using the text parameter after the url;
EX:https://my-worker-name.workers.dev/?text=hi mom!
.
So I'm going to use Django to send the admin a message when a user create an account.
...
### acount has been created
worker_url = 'https://my-worker-name.workers.dev/'
text = 'hi mom, someone just created a new account!'
try:
response = requests.get(f'{worker_url}?text={text}')
return JsonResponse(response.json(), status=response.status_code)
except requests.RequestException as e:
return JsonResponse({'error': str(e)}, status=500)
...
Don't forget to install "requests" by pip install requests
, and import both from django.http import JsonResponse
and import requests
Here's a more beginner friendly way of explaning everything https://unixnexo.github.io/src/blog/Using_Telegram_API_even_if_the_host_doesn't_let_you_to.html