Skip to main content

Tiny lib for icq/myteam command bots

Project description

mailru_im_command_bot

mailru_im_command_bot is convenient library for generic myteam/icq bots. In fact it is wrapper for mailru-im-bot.

It uses type annotations for help method and transforming arguments.

It is fully tested and production-ready)

Usage

You can create your bot with following code:

from mailru_im_command_bot import CommandBot, MessageEnv
from logging import getLogger
import enum


class ExampleEnum(enum.Enum):
    case_one = 1
    case_two = 2


bot = CommandBot(
    # you can provide any bot.Bot kwargs
    token='your_token_here',
    name='your_bot_name',
    version='your_bot_version',
    logger=getLogger(__name__),
    alert_to=['danila.fomin@corp.mail.ru'],
    help_message='your bot description',
)

@bot.command('example_command')
def example_command(
    env: MessageEnv,
    int_arg: int,  # required
    float_arg: float = 1.0,  # optional
    str_arg: str = 'test_str',  # optional
    enum_arg: ExampleEnum = ExampleEnum.case_one,  # optional
) -> str:
    """your function help message"""
    ...
    return 'your result'

bot.start()

You can also wrap existing bot:

from bot import Bot
from mailru_im_command_bot import CommandBot
from logging import getLogger

base_bot = Bot(
    token='your_token_here',
    name='your_bot_name',
    version='your_bot_version',
)

bot = CommandBot(
    from_bot=base_bot,
    logger=getLogger(__name__),
    alert_to=['danila.fomin@corp.mail.ru'],
    help_message='your bot description',
)

...

base_bot.start_polling()

Bot accepts messages like this:

/example_command 1
# you get int_arg = 1 and other arguments defaults

/example_command 1 0
# you get int_arg = 1, float_arg = 0.0 and other arguments defaults

...etc

As argument type you can use str, float, int and any enum.Enum. Library automatically validates and casts strings to your types.

Your help message will be like this:

your bot description

list of commands:
  /example_command
    your function help message
    args:
      int_arg: int
      float_arg: float = 1.0
      str_arg: str = test_str
      enum_arg: case_one|case_two = case_one

Bot automatically writes access log with provided logger.

[ACCESS] [user_id]@[chat_id] /example_command elapsed=0.1s

If an exception occured bot will write the error into log, send 'some error occured' to user and report error to users in alert_to list.

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

mailru-im-command-bot-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mailru_im_command_bot-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file mailru-im-command-bot-0.1.1.tar.gz.

File metadata

  • Download URL: mailru-im-command-bot-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.1 Darwin/20.3.0

File hashes

Hashes for mailru-im-command-bot-0.1.1.tar.gz
Algorithm Hash digest
SHA256 23bde914d438172b75ff9b1ba7ef311a5c192e9120fe369ac7e1d3b463ca772f
MD5 b1c8b6e334f84e103ae8cb41b8501f5b
BLAKE2b-256 81cc30253111aaf6cb3c429b26cc823bef9f7fb89497ecbe359006971931a7a3

See more details on using hashes here.

File details

Details for the file mailru_im_command_bot-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mailru_im_command_bot-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a00afbf392b941842151eb1fd7373708a238c6796accf4c00ed4a1102460d53
MD5 db7013908d899ca47afb1e10e2880e36
BLAKE2b-256 caffbf44afe85b47c38e25988bd30cb39ae3f9d898cf4f4c626989530f43527c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page