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

时间:2016-07-25 05:51:30

标签: python bots botconnector

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

1 个答案:

答案 0 :(得分:6)

是的,这是可能的。请结帐Microsoft bot built on Django (python web framework)以便实施。

下面是回复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"])
                       }
                    )

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