Skip to main content

PyTGram is python library to create Telegram Bot based on Twisted.

Installing

Prerequisites:

  • Python 3.x

You may need to install additional packages, depending on your operating system:

  • Debian/Ubuntu:

$ sudo apt-get install python3-dev libffi-dev libssl-dev
  • RHEL/CentOS (for python 3.6):

$ sudo yum install python36u-devel openssl-devel
$ sudo yum group install "Development Tools"
  • FreeBSD:

$ pkg install ca_root_nss

You can install pytgram using pip:

$ pip install pytgram

Usage

Webhook setup

from pytgram import set_webhook

set_webhook('bot_token', 'https://example.com')

If you use self-signed certificate - the pass path to it:

from pytgram import set_webhook

set_webhook('bot_token', 'https://example.com', 'path/to/cert.pem')

Creating a simple bot

import sys

from twisted.python import log
from twisted.internet import reactor, ssl
from twisted.internet.defer import inlineCallbacks

from pytgram import TelegramBot, MessageHandler, web_hook

bot = TelegramBot('bot_token')


@MessageHandler(content=['text'])
@inlineCallbacks
def text_handler(message):
    result = yield bot.send_message(chat_id=message.chat.id,
                                    text=message.text[::-1])
    print(result)


@MessageHandler(command=['start'])
@inlineCallbacks
def start_handler(message):
    result = yield bot.send_message(chat_id=message.chat.id,
                                    text='Received command "start"')
    print(result)


def main():
    log.startLogging(sys.stdout)
    ssl_context = ssl.DefaultOpenSSLContextFactory('ssl/secret.key',
                                                   'ssl/cert.pem')
    # If want use reverse proxy, then replace listenSSL on listenTCP.
    reactor.listenSSL(443, web_hook(bot.token), ssl_context)
    reactor.run()


if __name__ == '__main__':
    main()

If you want to use polling mode, you can do it like that:

import sys

from twisted.python import log
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks

from pytgram import TelegramBot, MessageHandler, polling

bot = TelegramBot('bot_token')


@MessageHandler(content=['text'])
@inlineCallbacks
def text_handler(message):
    result = yield bot.send_message(chat_id=message.chat.id,
                                    text=message.text[::-1])
    print(result)


@MessageHandler(command=['start'])
@inlineCallbacks
def start_handler(message):
    result = yield bot.send_message(chat_id=message.chat.id,
                                    text='Received command "start"')
    print(result)


def main():
    log.startLogging(sys.stdout)
    reactor.listenTCP(8080,  Site(Resource()))
    polling(bot, interval=10)
    reactor.run()


if __name__ == '__main__':
    main()

Metadata

Release files for pytgram 0.0.7

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

Source distribution (sdist)

Source distribution for pytgram 0.0.7
File Size Uploaded Provenance Actions
pytgram-0.0.7.tar.gz 23.5 MB Without provenance Details for pytgram-0.0.7.tar.gz

Release files / pytgram-0.0.7.tar.gz

Download URL pytgram-0.0.7.tar.gz
Size 23.5 MB
Tags Source
SHA-256 checksum
How to use checksums
efdddd5cdeaf2233459a4a4a3e81c6585bb5e3ef15c977a6c52e34068f91655d
BLAKE2b-256 checksum
How to use checksums
39d1bac1ee8e3c01b133a179752d5a03bacb2697a2f369aec5e79527b1db2cae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pytgram 0.0.7

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