从业务逻辑 Python 发送机器人框架 activity

send bot framework activity from business logic Python

我正在试用 Bot Framework SDK V4 Python GA。使用 LUIS 检测到意图后,我希望能够处理一些业务逻辑并做出响应。我希望能够在业务逻辑的同时发送消息 因为我想让用户知道逻辑正在处理中,需要他稍等片刻。我知道机器人通常不用于长 运行 流程,但我有一个需要这样做的用例。我试图将 turncontext 传递给业务逻辑并从那里发送消息,但它抛出了以下错误。

can't pickle coroutine objects

我是异步编程的新手,不确定这里到底发生了什么。以下是我尝试过的。我尝试通过将业务逻辑完全放在不同的 class 中来做同样的事情,但遇到了同样的问题。来自 on_message_activity 的初始消息运行良好,但是当尝试从企业发送消息时,它会抛出上述错误。我在这里错过了什么?

async def someUseCase(self,turncontext: TurnContext):
    await turncontext.send_activity(MessageFactory.text("Processing your query. Give me a moment."))
    output = someLongRunningBusinessLogic()
    return MessageFactory.text(output)

async def on_message_activity(self, turn_context: TurnContext):
    luisResult = await self.LuisRecog.recognize(turn_context) 
    print(luisResult.get_top_scoring_intent())
    intent = LuisRecognizer.top_intent(luisResult,min_score=0.40)
    if intent != "None":
        await turn_context.send_activity("processing your query...")
        return await turn_context.send_activity(self.someUseCase(turn_context))
    else:
        await turn_context.send_activity(MessageFactory.text("No intent detected."))

async def 函数 return 应等待的可等待对象。您遇到的错误很可能是因为您试图将协程传递给期望在该行上出现 activity 的函数:

return await turn_context.send_activity(self.someUseCase(turn_context))

send_activity 需要一个 activity 但 someUseCase return 是一个协程。

您可以在 Python 文档中阅读有关协程的更多信息:https://docs.python.org/3/library/asyncio-task.html