python - 如何格式化 python sdk 的 Bot 框架的输出消息?

标签 python botframework chatbot markup microsoft-teams

我正在尝试使用自定义函数的输出作为聊天机器人输出的消息。我正在使用此链接提供的示例 cookiecutter echo 模板。

有哪些方法可用于格式化聊天中可见的输出。例如粗体、斜体等。还有我如何使用不同的格式。将来 my_foo 的输出将是 json。

Echo Bot Python sdk template

自定义函数

def my_foo(text):
     return text.upper()

机器人功能

class MyBot(ActivityHandler):
    # See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.

    async def on_message_activity(self, turn_context: TurnContext):
        await turn_context.send_activity(f"Upper Case  { my_foo(turn_context.activity.text }")

    async def on_members_added_activity(
        self,
        members_added: ChannelAccount,
        turn_context: TurnContext
    ):
        for member_added in members_added:
            if member_added.id != turn_context.activity.recipient.id:
                await turn_context.send_activity("Hello and welcome!")

如果有一个文档澄清这一点或一些可以引用的基本步骤,那就完美了。

最佳答案

有关在 Teams 中设置消息格式所需的所有信息是 right here 。您可以使用 Markdown 或 XML。我将向您展示如何设置事件的可选 text_format 属性,但即使您不设置该属性,Teams 也足够智能来推断格式。

from botbuilder.schema import TextFormatTypes

. . .

async def on_message_activity(self, turn_context: TurnContext):
    markdown_reply = MessageFactory.text(
        f"Here is *italic* and **bold** with Markdown")
    xml_reply = MessageFactory.text(
        f"Here is <i>italic</i> and <b>bold</b> with XML")

    # markdown_reply.text_format = TextFormatTypes.markdown
    # xml_reply.text_format = TextFormatTypes.xml

    await turn_context.send_activity(markdown_reply)
    await turn_context.send_activity(xml_reply)

Teams

关于python - 如何格式化 python sdk 的 Bot 框架的输出消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796075/

相关文章:

c# - 在 FormFlows - Bot Framework 中为 Quit 添加另一个关键字

python - 使用matplotlib绘制图时,X轴刻度标签太密

python - 我可以在查询调用中动态更改 order_by 属性吗?

c# - MS BotFramework 数据保存,BotState

machine-learning - 如何训练对话流以提取值可能不会被空格分割

neural-network - CLIPS 与 NN 创建聊天机器人

python - NameError 在 python 中使用 execfile

python - 在测试中使用 url_for

node.js - 如何在 Microsoft Bot Framework 中通过 Skype for Business channel 获取用户的身份验证详细信息

botframework - 我们是否可以构建一个产品,使最终用户能够在自托管环境中使用机器人框架创建 session 聊天机器人?