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.3.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.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: telegram_gateway-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 b1d0e6e5ea2859916b2b4a842dd9d53e5929fd3b2213db5e09752dee4c64c91d
MD5 a9b75805355306447dd50759c542969f
BLAKE2b-256 c1aabde3a8f2ac980d2da2706990c28e84f2315b089420e3d1bd52b74a764b6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for telegram_gateway-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0ab889bfeba05d3021b6b2af93a105a1799c1b49bf43026713136e741121b5ef
MD5 15dee30cffdb53e2364bf6a7ca380bff
BLAKE2b-256 34282c7f93bdb739bcf43ec4552a8ab795622640b7b8153d214008161abc4cdb

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