Skip to content

Instantly share code, notes, and snippets.

@spookyahell
Last active October 17, 2024 14:27
Show Gist options
  • Save spookyahell/d2878b42e079bd7aea7f34005b4301e7 to your computer and use it in GitHub Desktop.
Save spookyahell/d2878b42e079bd7aea7f34005b4301e7 to your computer and use it in GitHub Desktop.
Example flow for Telegram Gateway usage | Telegram Gateway aims to replace SMS verification, it requires explicit user consent | Minimum Top up: 100 USD (in TON) via fragment.com (currently 18.8 TON)
# License: MIT (any other compatible license is fine, you may ask if you are uncertain, but probably: totally fine.)
# If you make use of this in a corporate setting, I'd appreciate being contacted for some tokens of appreciation, but it's optional.
# If you need a private way of getting in touch, you can try [email protected] (space is running low there)
# but in general, I'd prefer a more open, public approach, such as via comments below.
# If you found this anywhere else but spookyahell's Github Gists, it was "stolen" from the following link
# https://gist.github.com/spookyahell/d2878b42e079bd7aea7f34005b4301e7
import niquests as requests # best practice: don't use feature frozen packages, this way code will still run
# Initial testing was done with the #1 library:
# python-requests (and now I verified with niquests: And guess what? Works.)
# Compatible with:
# import requests
ROOT = 'https://gatewayapi.telegram.org/'
TOKEN = 'you need your own token' # Get your own at gateway.telegram.org
HEADERS = {
'Authorization': 'Bearer {0}'.format(TOKEN),
'Content-Type': 'application/json'
}
def checkSendAbility(phone_number):
ENDPOINT = ROOT + '/checkSendAbility'
json_body = {
'phone_number': phone_number
}
full_response = requests.post(ENDPOINT, headers = HEADERS, json = json_body).json()
if full_response['ok'] is True:
result = full_response['result']
request_id = result.get('request_id')
return True, request_id
else:
return False, full_response['error']
def sendVerificationMessage(
request_id,
phone_number,
code_length = 6,
sender_username = None
):
ENDPOINT = ROOT + '/sendVerificationMessage'
json_body = {
'request_id': request_id,
'phone_number': phone_number,
'code_length': code_length,
}
if sender_username:
json_body['sender_username'] = sender_username
full_response = requests.post(ENDPOINT, headers = HEADERS, json = json_body).json()
if full_response['ok'] is True:
result = full_response['result']
return True, result
else:
return False, full_response['error']
def checkVerificationStatus(request_id, code_provided_by_user):
ENDPOINT = ROOT + '/checkVerificationStatus'
json_body = {
'request_id': request_id,
'code': code_provided_by_user
}
full_response = requests.post(ENDPOINT, headers = HEADERS, json = json_body).json()
if full_response['ok'] is True:
result = full_response['result']
verification_status = result.get('verification_status')
return True, verification_status['status']
else:
return False, full_response['error']
if __name__ == '__main__':
phone_no = '49XXXXXXXXXXXXX' # For testing purposes you can send free requests to your own number.
print('Checking ability to send...')
is_ok, request_id = checkSendAbility(phone_no)
if is_ok is True:
print('Sending the request...')
is_ok, result = sendVerificationMessage(
request_id,
phone_no,
sender_username = 'MedienKommentare' # This is where you can put your own channel name, needs to be one you own, may need to be a verified one.
)
# During testing with my own number the "needs to be a verified channel" wasn't required
# Personally, I would prefer if that was the case throughout the whole program.
# Probably depends if there will be abuse, since the system is VERY new.
# They can immediately turn it on or perhaps already have for phone numbers that aren't your own (untested)
# It verifies if the channel ownership matches the account that owns the gateway account.
# I tried sending on behalf of "durov", guess he really didn't want that to be allowed - Makes sense.
askCodeFromUser = input('Code received: ')
is_ok, result = checkVerificationStatus(request_id, askCodeFromUser)
if result == 'code_valid':
print('SUCCESS: Code is valid.')
# Do whatever is necessary after successfuly validation here
else:
print('FAILED: Code not valid. ({0})'.format(result))
else:
print('ERROR: Failed. ({0})'.format(request_id))
@spookyahell
Copy link
Author

spookyahell commented Oct 14, 2024

Do not change the tab use, I use tab characters on purpose, by choice.
(I'm not even sure why I'm saying this, how would you change the code, this isn't like a full repo where you could submit pull requests.)

Why? I like to piss people off — Haha. Just kidding. But I don't mind if I do.
I prefer to have tabs in my browsers as well as in my code.
Thanks for understanding and have a good day.

If you're wondering about the remainder of "weird coding style" occurrances:
Code is meant to be compatible with Python 3.4 (Windows XP)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment