问题
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