Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Bot框架保存对列表或数组的引用_Python_Botframework - Fatal编程技术网

Python Bot框架保存对列表或数组的引用

Python Bot框架保存对列表或数组的引用,python,botframework,Python,Botframework,我对botCONVERSATION\u reference有问题,它只保存了一个CONVERSATION\u reference(最新版本)。我要拯救他们 比如说 然后我在Microsoft团队的聊天中调用或添加bot,代码保存conversation\u reference。如果我在另一个聊天或呼叫bot中添加bot,它看起来像覆盖conversation\u reference,并且我只有最新的conversation\u reference,但例如,如果我想向所有调用或添加bot的聊天发送

我对bot
CONVERSATION\u reference
有问题,它只保存了一个
CONVERSATION\u reference
(最新版本)。我要拯救他们

比如说 然后我在Microsoft团队的聊天中调用或添加bot,代码保存
conversation\u reference
。如果我在另一个聊天或呼叫bot中添加bot,它看起来像覆盖
conversation\u reference
,并且我只有最新的
conversation\u reference
,但例如,如果我想向所有调用或添加bot的聊天发送主动消息,它只会在最新的聊天中发送

所以我需要更改函数来将
conversation\u reference
保存到数组或列表,但我找不到正确的解决方案<代码>Bot.py

  def __init__(
    self, 
    conversation_references: Dict[list, ConversationReference],
    timestamp: str = None,
    channel_id: str = None,
    prompted_for_user_name: bool = False
):
    self.timestamp = timestamp
    self.channel_id = channel_id
    self.prompted_for_user_name = prompted_for_user_name
    self.conversation_references = conversation_references

   def _add_conversation_reference(self, activity: Activity):
        """
        This populates the shared Dictionary that holds conversation references. In this sample,
        this dictionary is used to send a message to members when /api/notify is hit.
        :param activity:
        :return:
        """
        conversation_reference = TurnContext.get_conversation_reference(activity)
        print(f"Adding new conversation to the list: {conversation_reference}")
        self.conversation_references[
            conversation_reference.user.id
        ] = conversation_reference
app.py
中,我也将其从
str
更改为
list

CONVERSATION_REFERENCES: Dict[list, ConversationReference] = dict()
def\u add\u conversation\u reference(self,activity:activity):
函数中,我尝试更改此行

conversation_reference = TurnContext.get_conversation_reference(activity)
在我试着像这样变换它之后

conversation_reference =[]
conversation_reference.append(TurnContext.get_conversation_reference(activity))
        return await callback(context)
  File "C:\Program Files\Python38\lib\site-packages\botbuilder\core\activity_handler.py", line 71, in on_turn
    await self.on_message_activity(turn_context)
  File "C:\bots\proactive_bot.py", line 52, in on_message_activity
    self._add_conversation_reference(turn_context.activity)
  File "C:\bots\proactive_bot.py", line 67, in _add_conversation_reference
    conversation_reference.user.id
AttributeError: 'list' object has no attribute 'user'
我犯了这样的错误

conversation_reference =[]
conversation_reference.append(TurnContext.get_conversation_reference(activity))
        return await callback(context)
  File "C:\Program Files\Python38\lib\site-packages\botbuilder\core\activity_handler.py", line 71, in on_turn
    await self.on_message_activity(turn_context)
  File "C:\bots\proactive_bot.py", line 52, in on_message_activity
    self._add_conversation_reference(turn_context.activity)
  File "C:\bots\proactive_bot.py", line 67, in _add_conversation_reference
    conversation_reference.user.id
AttributeError: 'list' object has no attribute 'user'
为什么我需要这个?因为我想做一个机器人,他可以向他所在的所有聊天室发送主动消息。但如果它只保存最新的
对话\u参考
,我只能向一个聊天室发送主动消息

对于主动消息发送,请输入
app.py
code

   async def _send_proactive_message():
        for conversation_reference in CONVERSATION_REFERENCES.values():
            print(f"converstion reference: {conversation_reference}")
            await ADAPTER.continue_conversation(
                conversation_reference,
                lambda turn_context: turn_context.send_activity(split),
                APP_ID,
            )
        print(CONVERSATION_REFERENCES.values())

有什么办法解决我的问题吗?谢谢。

希望您看起来像这样

  • 在app.py中定义 CONVER_REF:Dict[str,ConversationReference]=Dict()

  • 行业协会

  • self.conv\u ref=conver\u ref->pass as ctor argu

    3。成员上的异步定义\u添加了\u活动存储获取会话\u参考

            for conversation_reference in self.conv_ref.values(): 
            await self.adapter.continue_conversation(
            conversation_reference,
            lambda turn_context: turn_context.send_activity("proactive hello"),APPID,)
    
    con\u obj=TurnContext.get\u conversation\u引用(turn\u context.activity) self.conv\u ref[con\u obj.user.id]=TurnContext.get\u conversation\u引用(turn\u context.activity)

    4.主动功能执行

            for conversation_reference in self.conv_ref.values(): 
            await self.adapter.continue_conversation(
            conversation_reference,
            lambda turn_context: turn_context.send_activity("proactive hello"),APPID,)
    
    在我的示例中,我已经测试了用例,如果新用户连接到bot,我会向其他用户发送消息(只需调用主动功能进行测试)
    这里是视频示例:

    您说您做了更改。你对什么做了改变?您是否遵循特定的样本?你能链接到它吗?你读过这个吗@Kyle Delaney我不做重要的改变来保持谈话的安全。我举一个例子,我如何尝试用只保存一个会话引用(最新)的方法来解决问题。当然,我读了这篇文章,从中我为我的机器人选择了逻辑和代码。你读过文档很好,但看起来你没有遵循它。你能回答我的其他问题吗?你还在做这个吗?不,我在azure blob中存储对话引用对象,然后在主动消息功能中调用它谢谢帮助,但你的代码不起作用,首先在
    应用程序中如何调用
    self
    ,py
    ,第二个没有
    self
    它只发送一次聊天,不是像在video.define app.pyCONVERSATION\u引用中显示的那样的多个:Dict[str,ConversationReference]=Dict()在同一文件app.py下面定义函数async def\u send\u proactive\u message():对于conversation\u引用中的conversation\u引用。值():wait ADAPTER.continue\u conversation(对话参考,lambda turn\u context:turn\u context.send\u活动(“主动问候”),应用程序ID)但它是一样的,正如我从一开始得到的,它只保存一个con引用,然后我会尝试保存更多,它只是覆盖它。你能给我完整的bot代码吗?谢谢哪个函数得到覆盖它。