2016-07-25 113 views
2

我想写一个python机器人,我知道是否有可能将我的机器人连接到微软机器人连接器?如何将我的python机器人连接到微软机器人连接器

+1

您可以看看我一直在写的连接到Microsoft Bot连接器API的库。当我想为微软团队编写一个机器人时,我找不到任何真正简单的东西,所以我创建了自己的机器人。 https://github.com/Grungnie/microsoftbotframework –

回答

6

是的,这是可能的。请检查Microsoft bot built on Django (python web framework)执行。这里下面

是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

+0

任何想法,如果这可以用来连接到模拟器? (不是外部的微软机器人框架) – ShreyasG