Skip to main content

Interact with the Telegram Gateway API.

Project description

Telegram Gateway

Interact with the new Telegram Gateway API

How to use?

You need to create a telegram_gateway.TelegramGateway object, pass in the API key from the Telegram Gateway admin panel. After that, call one of the 4 methods available:

sendVerificationMessage

Send a verification message with this method. If you used checkSendAbility before calling this, pass in the request_id to avoid being billed twice.

Example usage: gateway.sendVerificationMessage('+xxxxxxxxxxxxxxx', code_length=8, request_id=sendAbilityResponse.request_id)

checkSendAbility

Check if you can send a verification code to a specific phone number. You will get billed, even if you don't send a verification code afterwards.

Example usage: gateway.checkSendAbility('+xxxxxxxxxxxxxxx')

This raises an error if you can't send a message to that phone number, so make sure you setup an exception handler, otherwise your production environment may crash after a user puts in an invalid phone number by mistake.

checkVerificationStatus

Use this method to check the status of a verification message that was sent previously. If the code was generated by Telegram for you, you can also verify the correctness of the code entered by the user using this method.

You can call this even if you made the code yourself - it will allow you to track the conversion rate in the Telegram Gateway admin panel.

Example usage: gateway.checkVerificationStatus(sendCodeResponse.request_id) Example usage (2): gateway.checkVerificationStatus(sendCodeResponse.request_id, code=input('Enter the code: ')).verification_status.status == telegram_gateway.enums.VerificationResult.CODE_VALID

revokeVerificationMessage

Use this method to revoke a verification message that was sent previously. However, this does not guarantee that the message will be deleted. For example, it will not be removed if the recipient has already read it.

Example usage: gateway.revokeVerificationMessage(sendCodeResponse.request_id)

This will raise an exception if the server returns False, even if it doesn't return any actual error, be aware of that.

Example code

Send a message, revoke after 5 seconds (using TTL):

import telegram_gateway

gateway = telegram_gateway.TelegramGateway(input("API token: "))
gateway.sendVerificationMessage(input("Phone number: "), code_length=8, ttl=5)

Revoke explicitly using the revokeVerificationMessage method:

import time

import telegram_gateway

gateway = telegram_gateway.TelegramGateway(input("API token: "))
response = gateway.sendVerificationMessage(input("Phone number: "), code_length=8)
time.sleep(5)
gateway.revokeVerificationMessage(response.request_id)

Send a code, then validate it:

import telegram_gateway

gateway = telegram_gateway.TelegramGateway(input("API token: "))
# This works with both a Telegram provided code and your own code.
sendCodeResponse = gateway.sendVerificationMessage(input("Phone number"), code_length=8)
code = input("Enter code: ")
while True:
    response = gateway.checkVerificationStatus(sendCodeResponse.request_id, code=code)
    if response.verification_status.status in [telegram_gateway.enums.VerificationResult.EXPIRED, telegram_gateway.enums.VerificationResult.CODE_MAX_ATTEMPTS_EXCEEDED]:
        print("The code expired, or the maximum amount of attempts has been exceeded.")
        break
    if response.verification_status.status == telegram_gateway.enums.VerificationResult.CODE_VALID:
        print("You entered the correct code.")
        break
    print("You entered the wrong code, try again.")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_gateway-0.0.4.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_gateway-0.0.4-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file telegram_gateway-0.0.4.tar.gz.

File metadata

  • Download URL: telegram_gateway-0.0.4.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for telegram_gateway-0.0.4.tar.gz
Algorithm Hash digest
SHA256 560394990a4eba84e71afc35de4ead8462ca13083df2b6fdab5152072ea043ca
MD5 d8e1f3b5e140be196ecdfdd53301ee57
BLAKE2b-256 7ae9f78cbefbea035cc0e97f7176b8ee03f6037d83f17beb2fd5df94812386ee

See more details on using hashes here.

File details

Details for the file telegram_gateway-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_gateway-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f7fd0e337358700c6e11fe8ee99db9b1a622ee3a40ad381f8374c20d62aab3b3
MD5 cd819b3fa27516d3c0965e7da1eb46a7
BLAKE2b-256 46c5d0a00db4d62f3dbb98e42d01b40a7c17d1b4df118070a93970ef0747773f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page