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.

Files for pytgram, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size pytgram-0.0.1.tar.gz (23.5 MB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page