Botframework 发送主动消息然后字符串不为空

Botframework send proactive message then string not empty

我在使用 Python 的 Bot 框架发送主动消息时遇到问题。 首先,我需要从 Outlook 获取消息正文,然后机器人必须将其作为消息发送到添加它的所有聊天。

为此,我首先创建了一个新文件并将其命名为 Email.py.

为了读取每条传入的消息正文,我只使用了 while true:time.sleep()

这是我的代码示例:

import imaplib, email, getpass
from email import policy
import json
import time

imap_host = 'outlook.office365.com'
imap_user = 'xx@xx.com'

# init imap connection
mail = imaplib.IMAP4_SSL(imap_host, 993)
rc, resp = mail.login(imap_user, 'xxxxxx')
while True:
    # select only unread messages from inbox
    mail.select('Inbox')
    status, data = mail.search(None, '(UNSEEN)')

   if not data[0].split():
         time.sleep(120) 
    # Bot message variable
    Message_for_bot = ''
    # for each e-mail messages
    for num in data[0].split():
        # get a single message and parse it by policy.SMTP (RFC compliant)
        status, data = mail.fetch(num, '(RFC822)')
        email_msg = data[0][1]
        email_msg = email.message_from_bytes(email_msg, policy=policy.SMTP)

    # print only message parts that contain text data
        for part in email_msg.walk():
            if part.get_content_type() == "text/plain":
                for line in part.get_content().splitlines():
                   Message_for_bot += '\n' + line             
        print(Message_for_bot)

在我成功创建一个程序来读取和打印所有收到的消息后,我尝试构建我的机器人。找了一个proactive message bot on the Internet,拿来做例子

首先我想 运行 这个文件在后台 os,但后来我的机器人没有 运行ning。因此,我尝试在 bot 文件中添加一个 async 函数,但它没有用。我的机器人只是忽略了这个功能。 (然后我在 activity_handler.py 中找到了异步函数,但我没有找到任何可以帮助我的函数。)

然后我尝试添加一个 on_message_activity 函数,并认为如果我在 Teams 中像“@bot hi”这样调用机器人,它可能会开始工作。对于这个想法,我必须始终 运行 while 循环并且永远不要停止机器人,但是我只是收到一条消息,如果有新的传入消息,那么机器人就不会再写了,这不是解决方案因为如果 bot 用于多个聊天,那么它根本无法以这种方式工作。

然后我尝试将我的代码包含在 on_members_added_activity 中,它似乎可以完美地在网络聊天中进行 azure 测试,但在 1-2 条消息停止工作后的团队中。 我的代码

async def on_members_added_activity(
        self, members_added: [ChannelAccount], turn_context: TurnContext
    ):
    
        imap_host = 'outlook.office365.com'
        imap_user = 'xxxxxx@xxxxxx.com'

        # init imap connection
        mail = imaplib.IMAP4_SSL(imap_host, 993)
        rc, resp = mail.login(imap_user, 'xxxxxx')
        while True:
            # select only unread messages from inbox
            mail.select('Inbox')
            status, data = mail.search(None, '(UNSEEN)')

            if not data[0].split():
                time.sleep(5) 
            # Bot message variable
            Message_for_bot = ''
            # for each e-mail messages
            for num in data[0].split():
                # get a single message and parse it by policy.SMTP (RFC compliant)
                status, data = mail.fetch(num, '(RFC822)')
                email_msg = data[0][1]
                email_msg = email.message_from_bytes(email_msg, policy=policy.SMTP)

            # print only message parts that contain text data
                for part in email_msg.walk():
                 if part.get_content_type() == "text/plain":
                    for line in part.get_content().splitlines():
                        Message_for_bot += '\n' + line       
                                
                await turn_context.send_activity(f"{Message_for_bot}")
        for member in members_added:
            if member.id != turn_context.activity.recipient.id:
                await turn_context.send_activity(
                    "bot starting work..."
                )
 
 

因此,只要 Message_for_bot 不为空,就可以向添加机器人的任何地方发送消息(它需要以某种方式获取此信息,也许它保存在机器人内存中)。

我们将不胜感激。

正如我们所讨论的,一些逻辑必须改变

  1. 将您的代码移出 on_members_added_activity 函数
  2. 使用主动概念发送消息

-维诺斯