我根据文档搭建了一个基于Python的小型Azure Bot
机器人主要做什么?它只是自动回复 Teams 频道中对特定关键词做出反应的消息,这是代码
from botbuilder.core import ActivityHandler, TurnContext
class MyBot(ActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
user_message = turn_context.activity.text.lower()
lock_keywords = ['locked', 'unlock', 'blocked', 'locking', 'lock', 'login', 'connect', 'vpn']
password_keywords = ['password', 'expired', 'credentials']
duo_keywords = ['duo', '2fa']
# Convert user's message to lowercase for case-insensitive matching
user_message_lower = user_message.lower()
# Check for specific keyword groups and send corresponding responses
if any(word in user_message_lower for word in lock_keywords):
response_message = ("Hi,\n\n"
"If you are enrolled to SSPR you can now unlock yourself with this link:\n"
"https://confluence.infobip.com/display/CWT/Self+service+password+reset+-+add+additional+sign-in+method\n\n"
"If you are not enrolled, here is the link to enroll:\n"
"https://confluence.infobip.com/display/CWT/Self+service+password+reset+-+add+additional+sign-in+method\n\n"
"If it helped, write resolved, if not, write help")
await turn_context.send_activity(response_message)
if any(word in user_message_lower for word in password_keywords):
await turn_context.send_activity("If your password is expired you can reset it using this link: https://account.activedirectory.windowsazure.com/ChangePassword.aspx")
if any(word in user_message_lower for word in duo_keywords):
await turn_context.send_activity("If you need to re-enroll in DUO 2FA, please raise a ticket here: https://jira.infobip.com/")
问题是如何仅回复 1 次,以便在发布后机器人不会继续回复对 VPN 或密码等关键字做出反应的消息,如您从屏幕截图中看到的那样
我尝试了什么?
import logging
from botbuilder.core import ActivityHandler, TurnContext
# Configure logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # Set the logging level to DEBUG
class MyBot(ActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
# Check if the activity is from Microsoft Teams
if turn_context.activity.channel_id != 'msteams':
return # Exit the function to skip processing
# Check if the activity is a channel message (not a thread)
if turn_context.activity.conversation.conversation_type != 'channel':
return # Exit the function to skip processing
# Check if the activity is a reply (part of a thread)
if turn_context.activity.reply_to_id:
# This is a thread message
logger.info("It is a thread message")
return
# This is a top-level channel message (post)
logger.info("It is a post message")
..
机器人仍然无法区分频道帖子和线程
要使用 Azure 机器人在 Teams 频道帖子中仅回答一次,您可以按照以下步骤操作:
检测消息范围,判断是否为频道消息。
检查该消息是否是对现有线程的回复。您可以通过检查消息的 conversation.id 属性来完成此操作。如果包含该线程中第一条消息的 ID,则表示该消息是对该线程的回复。
如果消息是对线程的回复,您可以使用 .NET 中的 ReplyToActivity 方法或 Node.js 中的 session.send 方法向线程发送回复。请务必在回复中包含 conversation.id,以确保其发布在正确的线程中。
如果该消息不是对线程的回复,您可以忽略它或根据机器人的逻辑进行不同的处理。
请注意,具体实施细节可能会有所不同,具体取决于您用于构建 Azure 机器人的编程语言和框架。