router for chatbot
Project description
Chatrouter is an enhanced router for chatbots and easily integrates with any bot library.
key features
- 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)
- Readable route.
@chatbot.add_command("call me {my_name}")
# or
chatbot.add_command("call me {my_name} and {my_friend}")
# etc
- case sensitive and insensitive.
default case is insensitive
but you can add strict=True
to a route/command to make it case sensitive.
- public and private command.
command start with "/" and have description is public command, for example:
@chatbot.add_command("/test", description="test command", strict=True)
- invoke callback anywhere.
func = chatrouter.util.get_func("group_name", "command_name")
-
auto generated
/start
and/help
command. -
object storage
chatrouter.data_user
. -
support asynchronous.
-
support midleware
@chatbot.midleware()
def limit_user():
allowed_user_id = (123, 456)
return chatrouter.data_user.id in allowed_user_id
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)
asynchronous example
#-*-coding:utf8;-*-
import asyncio
import chatrouter
chatbot = chatrouter.group("test", asynchronous=True)
@chatbot.add_command("call me {name}")
async def test(name):
return f"hello {name}!"
async def main():
user_input = "call me human"
response = await chatrouter.async_run(chatbot, user_input)
print(response)
if __name__ == '__main__':
asyncio.run(main())
for more complex example, please open demo/telegram_bot.
Project details
Release history Release notifications | RSS feed
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.3.tar.gz
(4.4 kB
view hashes)
Built Distribution
Close
Hashes for chatrouter-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa428bbdce6f3c933dc49cde342e374f905433cbe974841c2a3b12e3b4d71cd7 |
|
MD5 | 6ec08b4cb89affa9006789d0e98c9922 |
|
BLAKE2b-256 | 99a1489d10ee1e2f1d172a5801e8c4e83021ec99d404cc7969a244e0f1763843 |