Skip to main content

Router for chatbot

Project description

Chatrouter is an enhanced router for chatbots and easily integrates with any bot library.

key features

  1. turned complexity into simplicity.

from:

if user_session == "A":
    ...
elif user_session == "B":
    ...
elif user_session == "C":
    ...
else:
    ...

to:

chatbot = chatrouter.group(user_session)
r = chatrouter.run(chatbot, msg)
  1. Readable route.
@chatbot.add_command("call me {my_name}")
# or 
chatbot.add_command("call me {my_name} and {my_friend}")
# etc
  1. case sensitive and insensitive.

default case is insensitive but you can add strict=True to a route/command to make it case sensitive.

  1. public and private command.

command start with "/" and have description is public command, for example:

@chatbot.add_command("/test", description="test command", strict=True)
  1. invoke callback anywhere.
func = chatrouter.util.get_func("group_name", "command_name")
  1. auto generated /start and /help command.

  2. object storage chatrouter.data_user.

installation

pip install chatrouter

quick example

# -*-coding:utf8;-*-
import chatrouter


chatbot = chatrouter.group("test", "this is test bot!")


@chatbot.add_command("call me {name}")
def say_handler(name):
    return f"hello {name}, nice to meet you!"


@chatbot.add_command("repeat me {one} and {two}")
def repeat_handler(one, two):
    return f"ok {one}.. {two}"


@chatbot.add_default_command()
def default_handler(command):
    return f"command {command} not found!"


if __name__ == '__main__':
    print(chatrouter.run(chatbot, "/start"))
    while True:
        try:
            i = input("you: ")
            r = chatrouter.run(chatbot, i)
            print(f"bot: {r}")
        except BaseException as e:
            print("bot: byebye!")
            exit(0)

for more complex example, please open demo/telegram_bot.

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

chatrouter-1.0.4.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file chatrouter-1.0.4.tar.gz.

File metadata

  • Download URL: chatrouter-1.0.4.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for chatrouter-1.0.4.tar.gz
Algorithm Hash digest
SHA256 4b366d90bc69c5beee87e0a5a6de0e7e26403fb8b0c1cd28daafcc335386a91c
MD5 2364de97a2770099184f1077d7be3e52
BLAKE2b-256 2146495ee766ed799016c74214c1fdb459579eeabc445cc6d47e091d7b83dc63

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