Skip to main content

TGGateway - Python Telegram Gateway API Wrapper

TGGateway is a Python library that provides both synchronous and asynchronous API clients for interacting with the Telegram Gateway API. This wrapper simplifies the process of sending verification messages, checking their status, and handling delivery reports.

Features

  • The one and only requirement is httpx.
  • Synchronous API client (TGGateway) using httpx.Client.
  • Asynchronous API client (AsyncTGGateway) using httpx.AsyncClient.
  • Handles API requests and responses, including:
    • Sending verification messages
    • Checking the status of sent messages
    • Checking delivery status and revoking verification messages
  • Custom exception handling for API errors.

Installation

You can install the package via pip:

python3 -m  pip  install  PyTGGateway

Usage

It is recommended to use TGGateway as a context manager. By doing this, you can make sure that connections are correctly cleaned up when leaving the with block. However, you also have the option to use .close() to explicitly close the connection.

Get access_token from Telegram Gateway Account.

Sync Example

As context manager

from TGGateway import TGGateway

def main():
    with TGGateway('access_token') as gateway:
        try:
            result = gateway.sendVerificationMessage(
                phone_number = '+1234567890',
                code = '0527'
            )
            print(result.request_id)
            print(result.request_cost)
        except Exception as e:
            print(f"Error: {e}")

if __name__ == '__main__':
    main()

As .close()

from TGGateway import TGGateway

def main():
    try:
        gateway = TGGateway('access_token')
        result = gateway.sendVerificationMessage(
            phone_number = '+1234567890',
            code = '0527'
        )
        print(result.request_id)
        print(result.request_cost)
    except Exception as e:
        print(f"Error: {e}")
    finally:
        gateway.close()

if __name__ == '__main__':
    main()

Async Example

As context manager

import asyncio
from TGGateway import AsyncTGGateway

async def main():
    async with AsyncTGGateway('access_token') as gateway:
        try:
            result = await gateway.sendVerificationMessage(
                phone_number = '+1234567890',
                code = '0527'
            )
            print(result.request_id)
            print(result.request_cost)
        except Exception as e:
            print(f"Error: {e}")

if __name__ == '__main__':
    asyncio.run(main())

As .close()

import asyncio
from TGGateway import AsyncTGGateway

async def main():
    try:
        gateway = AsyncTGGateway('access_token')
        result = await gateway.sendVerificationMessage(
            phone_number = '+1234567890',
            code = '0527'
        )
        print(result.request_id)
        print(result.request_cost)
    except Exception as e:
        print(f"Error: {e}")
    finally:
        await gateway.close()

if __name__ == '__main__':
    asyncio.run(main())

API Reference

For detailed API references, check the Official API Documentation.

TGGateway

Methods:

  • getAccessToken() -> str
    Get the current access token that is being used.

  • sendVerificationMessage(phone_number: str, request_id: str, ...) -> RequestStatus
    Sends a verification message to a phone number.

  • checkSendAbility(phone_number: str) -> RequestStatus
    Checks if a phone number can receive a verification message.

  • checkVerificationStatus(request_id: str, code: str = None) -> RequestStatus
    Checks the status of the verification process.

  • revokeVerificationMessage(request_id: str) -> bool
    Revokes a verification message.

Exception:

  • TGGatewayException
    Base class for all Telegram Gateway exceptions.

  • ApiError
    Raised when the API returns an error.

  • ResponseNotOk
    Raised when the response from the API is not successful (status code not 2xx).

AsyncTGGateway

The asynchronous version of the TGGateway class supports the same methods as its synchronous counterpart, but all methods must be awaited.

Contributing

Contributions are welcome! Please open an issue or submit a pull request with your improvements.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

With ❤️ Made By @Sasivarnasarma

Metadata

Release files for PyTGGateway 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyTGGateway 2.0.0
File Size Uploaded Provenance
pytggateway-2.0.0.tar.gz 8.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for PyTGGateway 2.0.0
File Interpreter ABI Platform
pytggateway-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / pytggateway-2.0.0.tar.gz

Download URL pytggateway-2.0.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
470594b55723477c9ddec575022c22290898ee9185496355f4e8bcad4bd34b6e
BLAKE2b-256 checksum
How to use checksums
f398c118a27f23711cb3f48d1c0188850ab095fad9ef3cbcf3f1aa51ec41fe4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / pytggateway-2.0.0-py3-none-any.whl

Download URL pytggateway-2.0.0-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0263fc032913c5c52d76f046b49fac1cb05ffe0aad970283b17bd0594a773c67
BLAKE2b-256 checksum
How to use checksums
a195a205d51eaa88fa1cf908d1fbd6bc3101ec340fb3b15dc0fbfb258f5774c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Security for PyTGGateway 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page