Skip to main content

Library to create Telegram Bot based on Twisted

Project description

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()

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

pytgram-0.0.7.tar.gz (23.5 MB view details)

Uploaded Source

File details

Details for the file pytgram-0.0.7.tar.gz.

File metadata

  • Download URL: pytgram-0.0.7.tar.gz
  • Upload date:
  • Size: 23.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pytgram-0.0.7.tar.gz
Algorithm Hash digest
SHA256 efdddd5cdeaf2233459a4a4a3e81c6585bb5e3ef15c977a6c52e34068f91655d
MD5 ca8d64ffbb856ac4222a0702fed01a67
BLAKE2b-256 39d1bac1ee8e3c01b133a179752d5a03bacb2697a2f369aec5e79527b1db2cae

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