How to connect my python bot to microsoft bot connector

◇◆丶佛笑我妖孽 提交于 2020-12-06 08:10:10

问题


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


回答1:


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

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"])
                       }
                    )

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



来源:https://stackoverflow.com/questions/38560546/how-to-connect-my-python-bot-to-microsoft-bot-connector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!