Skip to main content

Telegram Gateway API Wrapper

Project description

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

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

pytggateway-1.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

PyTGGateway-1.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pytggateway-1.0.0.tar.gz.

File metadata

  • Download URL: pytggateway-1.0.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for pytggateway-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8ddadd049470479592b2c9a9b1f5bbbfda0e8360f901eb9a4a5922e28207215f
MD5 43ab755ffa163586dad21d7dfbe5920d
BLAKE2b-256 3bc829169ebdff36b1419b5a36daf9afe06dcb8e3ca8fe557ccc8f46e9abeb22

See more details on using hashes here.

File details

Details for the file PyTGGateway-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: PyTGGateway-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for PyTGGateway-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1377bf680e3e33a6fbc5ca16548b6f29ead1eb6d4e68e947fdd0439c150a5ca
MD5 58fa662019524c594a95abc7a10ad15a
BLAKE2b-256 5f3c329c9ebe2a12baf7819729fc49fd7e6a9ffdeab8d5ee5635dc76d4cea89b

See more details on using hashes here.

Supported by

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