BotFramework v4主动消息仅适用于某些情况

2024-06-10 23:34:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我的团队用python和botframework开发的机器人有一种奇怪的行为。我将开始说明一般行为:

  • 机器人每天通过自适应卡提出两个问题并给出答案(单个或多个)
  • 用户可以回答是否感兴趣
  • 第二天,机器人将前一天问题的答案发送给回答问题的用户,然后再向所有订阅该频道的用户发送两个问题。(这是通过主动消息完成的)

问题来了:我保存了一个包含id\u user:conversation\u引用的字典,并在需要联系用户时检索它。消息中关于前一天问题答案的部分到达所有正确回答的用户,而消息中关于当天问题的部分仅到达少数用户。 那些收到当天问题的人可能回答了也可能没有回答前一天的问题。有这样一种情况,用户每天都回答问题,但是没有收到新问题的通知,但是有一个命令来检索这些问题。 我附上前一天答案的代码

async def _send_proactive_message(id_context, key, result, conv_dict):
    file_name = utils._filename_from_context(id_context[key])
    _, _, question, _, _, _, correct_answer, expl, img1, img2= 
       utils.get_question(key, file_name)
    expl_str = str(expl)
    if(expl_str == 'None'):
        expl_str = 'Nothing'
    correct_answer_str = str(correct_answer)
    for conversation_reference in conv_dict.values():
        if(conversation_reference.user.id in result.keys()):
            if (set(result[conversation_reference.user.id].split(',')) == 
               set(correct_answer_str.split(';'))):
                AppCredentials.trust_service_url(conversation_reference.service_url)
                await ADAPTER.continue_conversation(
                    conversation_reference,
                    lambda turn_context: turn_context.send_activities([
                    Activity(
                        text="bla bla bla",
                        type=ActivityTypes.message,
                        attachments=[CardFactory.adaptive_card(cards[0])]
                    )
                ]),
                    APP_ID,
                )

现在是当天问题的代码,以前的格式是一样的,但我仍然遇到上面列出的问题

async def _send_question_of_the_day(conv_dict):
    cards = await utils._create_adaptive_card_attachment()
    now = datetime.now()
    data = now.strftime('%d/%m/%Y')
    for conversation_reference in conv_dict.values(): 
        AppCredentials.trust_service_url(conversation_reference.service_url)
        try:
            await asyncio.wait_for(_send_qptd_sep(cards, data, conversation_reference), 
               timeout=5)
        except asyncio.TimeoutError:
            pass           

async def _send_qptd_sep(cards, data, conversation_reference):
    await ADAPTER.continue_conversation(
        conversation_reference,
        lambda turn_context: turn_context.send_activities([
        Activity(
            text="bla bla bla",
            type=ActivityTypes.message,
            attachments=[CardFactory.adaptive_card(cards[0])]
        ),
        Activity(
            type=ActivityTypes.message,
            attachments=[CardFactory.adaptive_card(cards[-1])]
        )
    ]),
        APP_ID,
    )

我使用这种超时方法是因为我担心,通过向每个人发送问题,会有人的对话引用已损坏,因此该方法将失败,for循环将无法继续。 但无论如何,这些信息都不会发送给所有人。提前感谢您的回答


Tags: 答案用户sendidmessagecontextdictcards