From ee1e098114ea2f165c278aa12c34a7a66e1a6e06 Mon Sep 17 00:00:00 2001 From: Roman Zlobin Date: Sat, 28 Sep 2024 01:10:16 +0300 Subject: [PATCH] update template to version 0.9 --- services/bot/bot/custom/conditions.py | 7 ++--- services/bot/bot/pipeline.py | 14 +++++----- services/bot/bot/script.py | 37 ++++++++++++++++----------- services/bot/requirements.txt | 2 +- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/services/bot/bot/custom/conditions.py b/services/bot/bot/custom/conditions.py index 000d740..9167f47 100644 --- a/services/bot/bot/custom/conditions.py +++ b/services/bot/bot/custom/conditions.py @@ -1,5 +1,6 @@ -from chatsky.script import Context +from chatsky import Context, BaseCondition -def is_upper_case(ctx: Context, pipeline): - return ctx.last_request.text.isupper() +class IsUpperCase(BaseCondition): + async def call(self, ctx: Context) -> bool: + return ctx.last_request.text.isupper() diff --git a/services/bot/bot/pipeline.py b/services/bot/bot/pipeline.py index b0a3b66..f868cdd 100644 --- a/services/bot/bot/pipeline.py +++ b/services/bot/bot/pipeline.py @@ -1,6 +1,6 @@ import os -from chatsky.pipeline import Pipeline +from chatsky import Pipeline from chatsky.context_storages import context_storage_factory from chatsky.messengers.telegram import LongpollingInterface @@ -27,12 +27,12 @@ def get_variable(var_name: str): messenger_interface = LongpollingInterface(token=TG_BOT_TOKEN) -pipeline = Pipeline.from_script( - getattr(script, "SCRIPT"), - start_label=getattr(script, "START_NODE"), - fallback_label=getattr(script, "FALLBACK_NODE", None), - pre_services=getattr(script, "PRE_SERVICES", []), - post_services=getattr(script, "POST_SERVICES", []), +pipeline = Pipeline( + script=script.SCRIPT, + start_label=script.START_NODE, + fallback_label=script.FALLBACK_NODE, + pre_services=script.PRE_SERVICES, + post_services=script.POST_SERVICES, context_storage=context_storage_factory(DB_URI) if DB_URI else None, messenger_interface=messenger_interface, ) diff --git a/services/bot/bot/script.py b/services/bot/bot/script.py index 9473119..a494b8b 100644 --- a/services/bot/bot/script.py +++ b/services/bot/bot/script.py @@ -1,8 +1,7 @@ -from chatsky.script import Message -from chatsky.script import RESPONSE, TRANSITIONS, LOCAL -from chatsky.script.conditions import exact_match, true +from chatsky import Message, RESPONSE, TRANSITIONS, LOCAL, Transition as Tr +import chatsky.conditions as cnd -from .custom.conditions import is_upper_case +from .custom.conditions import IsUpperCase from .custom.services import pre_service, post_service @@ -10,29 +9,37 @@ SCRIPT = { "technical_flow": { "start_node": { - TRANSITIONS: { - ("main_flow", "greeting_node"): exact_match("/start"), - }, + TRANSITIONS: [ + Tr( + dst=("main_flow", "greeting_node"), + cnd=cnd.ExactMatch("/start") + ) + ], }, "fallback": { - RESPONSE: Message("Error."), + RESPONSE: "Error.", }, }, "main_flow": { LOCAL: { - TRANSITIONS: { - "upper": is_upper_case, - "response": true(), - }, + TRANSITIONS: [ + Tr( + dst="upper", + cnd=IsUpperCase() + ), + Tr( + dst="response", + ) + ] }, "greeting_node": { - RESPONSE: Message("Hi, please say something."), + RESPONSE: "Hi, please say something.", }, "upper": { - RESPONSE: Message("Don't scream, please."), + RESPONSE: "Don't scream, please.", }, "response": { - RESPONSE: Message("Thank you."), + RESPONSE: "Thank you.", }, } } diff --git a/services/bot/requirements.txt b/services/bot/requirements.txt index 397ec3a..ce6f5b8 100644 --- a/services/bot/requirements.txt +++ b/services/bot/requirements.txt @@ -1,2 +1,2 @@ -chatsky[telegram,sqlite]~=0.8 +chatsky[telegram,sqlite]~=0.9 pytest \ No newline at end of file