如何将我的python bot连接到microsoft bot连接器

Nim*_*ima 2 python bots botconnector

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

Mr.*_*. A 6

是的,这是可能的.请检查基于Django(python web框架)构建的Microsoft bot以实现.

下面是一个回复Microsoft bot连接器的python代码

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"])
                       }
                    )
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,请更换<Microsoft App ID><Microsoft App Secret>使用适当的App IDApp secret.更多API签出Microsoft Bot Connector REST API - v3.0