2

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

4

1 回答 1

7

是的,这是可能的。请查看基于 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"])
                       }
                    )

在上面的例子中,请用适当的and替换<Microsoft App ID>and 。更多 API 结帐Microsoft Bot Connector REST API - v3.0<Microsoft App Secret>App IDApp secret

于 2016-09-05T10:20:19.467 回答