Skip to content

artcom-net/pytgram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyTGram

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

About

Library to create Telegram Bot based on Twisted.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages