首页 > 解决方案 > BotFramework v4 主动消息仅适用于某些

问题描述

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

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

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,
    )

我使用了这种超时方法,因为我担心通过向每个人发送问题会有一个损坏的 conversation_reference 的人,因此该方法会失败并且 for 循环不会继续。但无论如何,这些消息不会发送给所有人。预先感谢您的回答。

标签: pythonbotframeworkpython-asynciomicrosoft-teams

解决方案


推荐阅读