如何将python bot连接到Microsoft bot连接器 [英] How to connect my python bot to microsoft bot connector

查看:75
本文介绍了如何将python bot连接到Microsoft bot连接器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个python机器人,我知道是否可以将我的机器人连接到Microsoft bot连接器?

I want to write a python bot and I know if it is possible to connect my bot to microsoft bot connector ?

推荐答案

是的.请检出基于Django(python网络框架)构建的Microsoft机器人以进行实施.

Yes it's possible. Please checkout Microsoft bot built on Django (python web framework) for implementation.

以下是用于回复Microsoft bot连接器的python代码

Here below is a python code to reply back to Microsoft bot connector

import requests
app_client_id = `<Microsoft App ID>`
app_client_secret = `<Microsoft App Secret>`
def sendMessage(serviceUrl,channelId,replyToId,fromData, recipientData,message,messageType,conversation):
    url="https://login.microsoftonline.com/common/oauth2/v2.0/token"
    data = {"grant_type":"client_credentials",
        "client_id":app_client_id,
        "client_secret":app_client_secret,
        "scope":"https://graph.microsoft.com/.default"
       }
    response = requests.post(url,data)
    resData = response.json()
    responseURL = serviceUrl + "v3/conversations/%s/activities/%s" % (conversation["id"],replyToId)
    chatresponse = requests.post(
                       responseURL,
                       json={
                        "type": messageType,
                        "timestamp": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%zZ"),
                        "from": fromData,
                        "conversation": conversation,
                        "recipient": recipientData,
                        "text": message,
                        "replyToId": replyToId
                       },
                       headers={
                           "Authorization":"%s %s" % (resData["token_type"],resData["access_token"])
                       }
                    )

在上面的示例中,请将< Microsoft App ID> < Microsoft App Secret> 替换为适当的 App ID 应用秘密.以获得更多API检出 Microsoft Bot Connector REST API-v3.0

In the above example please replace <Microsoft App ID> and <Microsoft App Secret> with appropriate App ID and App secret. for more API checkout Microsoft Bot Connector REST API - v3.0

这篇关于如何将python bot连接到Microsoft bot连接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆