Skip to main content

Library for creating bots for telegram with Python.

Project description

Library for creating bots for telegram with Python.

Usage example

Here goes a simple example of a bot:

from sys import argv
from time import sleep

from origamibot import OrigamiBot as Bot


class BotsCommands:
    def __init__(self, bot: Bot):  # Can initialize however you like
        self.bot = bot

    def start(self, message):   # /start command
        self.bot.send_message(message.chat.id,
            'Hello user!\nThis is an example bot.')

    def echo(self, message, value: str):  # /echo [value: str] command
        self.bot.send_message(message.chat.id, value)

    def add(self, message, a: float, b: float):   # /add [a: float] [b: float] command
        self.bot.send_message(message.chat.id, str(a + b))


if __name__ == '__main__':
    token = (argv[1]
             if len(argv) > 1 else 
             input('Enter bot token: '))
    bot = Bot(token)
    bot.add_commands(BotsCommands(bot))
    bot.start()   # start bot's threads
    while True:
        sleep(1)
        # Can also do some useful work i main thread
        # Like autoposting to channels for example

Commands are added as methods of an object(be it class or instance of it), if their names don't start with _ which makes it possible to also contain some utility functions inside command container.

For the command to be called two conditions must be met:

  1. command name must match with method name
  2. command's arguments must match signature of a method

Method signature supports any number of arguments with simple typing(str, int, float, bool) or without a typing(in this case all arguments are strings by default), as well as variable number of arguments *args. More complex types(as lists, tuples, custom object classes) are not supported, as bot does not know how to parse them, and I don't want to enforce my own parsing algorithm, but bot will still attempt to convert it like cls(argument), but a correct result is not guaranteed.

Boolean values are considered True if their string representation is in {'True', 'true', '1'}, and False if in {'False', 'false', '0'}

TODO

  1. Add support for inline mode
  2. Process general messages without commands too
  3. Process events like chat member join/leave, etc

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

origamibot-0.2.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

origamibot-0.2.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file origamibot-0.2.0.tar.gz.

File metadata

  • Download URL: origamibot-0.2.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for origamibot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0996b1396cfa67d50a145a063aecfc8115b4f5b9d81f805fb1c3b420afeefde2
MD5 93c61a7891feda634e70dbe8d347e0bc
BLAKE2b-256 323a8aa0344c3679cd193bce01df89b29afaa7a30b7da92faa85dc208af81849

See more details on using hashes here.

File details

Details for the file origamibot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: origamibot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for origamibot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbd9fe72143b7e222d924b491b68dcf7be6a0ac377d931b5b8c4f51c3fe0cdff
MD5 e316297b5cf87554d633a76807d62f16
BLAKE2b-256 4688136c6df5fc5be5dd99aa8d55c316fe5f8053c29390350bedb7167236082d

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