Skype REST API python开始对话

时间:2017-09-15 21:50:52

标签: python skype-bots

刚刚将我的机器人Jessie添加到了联系人。现在尝试开始对话,什么都没有效果

import requests
import requests.auth as auth
import json

url = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
headers = {'Content-Type' : 'application/x-www-form-urlencoded', 'Host' : 'login.microsoftonline.com' }

r = requests.post(url, data="grant_type=client_credentials&client_id=ID&client_secret=SECRET&scope=https%3A%2F%2Fapi.botframework.com%2F.default")
print(r.content)

jsonAuth = json.loads(r.content)

print(jsonAuth['token_type'] + ' ' + jsonAuth['access_token'])

headers2 = {'Authorization' : 'Bearer ' + jsonAuth['access_token'], 'Content-Type':'application/json' }


url = "https://smba.trafficmanager.net/apis/v3/conversations"

user = {}
user['bot'] = {}
user['bot']['id']='7444e829-f753-4f97-95c9-8c33e79087d0'
user['bot']['name']='Jessie'

user['isGroup']=False
user['members']= []
user['members'].append({'id' : 'bogdan_danson', 'name' : 'b2soft'})

user['topicName'] = 'New Alert!'

jsonRequestBody = json.dumps(user)

print(jsonRequestBody)


req = requests.post(url, headers=headers2, data=jsonRequestBody)
print(req.content)

我得到回应:

b'{"error":{"code":"BadSyntax","message":"Bad format of conversation ID"}}'

我做错了什么?我需要其他流量或用户ID吗? Bot尚未发布,只是试图测试它。

我想与用户开始对话,然后在聊天或与用户分组中阅读/回答

1 个答案:

答案 0 :(得分:1)

目前尚不清楚你是如何获得身份证的。据我所知,您似乎使用用户的显式Skype ID(bogdan_danson)代替user['members'][0]['id'],但我认为这不是您应该使用的ID。

当一个webhook被修复并且当一个人向你的机器人发送消息时,将播放这种类型的json:

{
    "channelId": "skype",
    "recipient": {
        "name": <name of the bot>,
        "id": <recipient id>
    },
    "entities": [
        {
            "type": "clientInfo",
            "country": "GB",
            "locale": "en-GB",
            "platform": "Mac"
        }
    ],
    "text": <some text>,
    "from": {
        "name": <name of the sender>,
        "id": <chat id>
    },
    "conversation": {
        "id": <conversation id>
    },
    "type": "message",
    "serviceUrl": <service url>,
    "channelData": {
        "text": "Some text"
    },
    "id": <id>,
    "timestamp": "2017-11-21T18:39:10.129Z"
}

现在,user['members'][0]['id']应该等于用户的<chat id>,而不是他/她的显式Skype ID。

从组中获取新用户的<chat id>

如果你的机器人被添加到一个组中,那么你的webhook必须收到这种类型的json对象:

{
  "id": <id>,
  "recipient": {
    "id": <bot id>,
    "name": <bot name>
  },
  "type": "conversationUpdate",
  "conversation": {
    "id": <conversation id>,
    "isGroup": true
  },
  "from": {
    "id": <from id>
  },
  "channelId": "skype",
  "membersAdded": [
    {
      "id": <chat id of member1>
    },
    {
      "id": <chat id of member2>
    },
    {
      "id": <chat id of member3>
    }
  ],
  "timestamp": "2017-12-03T21:12:01.328Z",
  "serviceUrl": <service url>
}

通过这种方式,您可以在将机器人添加到组中的同时获得该组成员的对话和聊天ID。

要根据API reference获取更新的成员(聊天ID和姓名),您可以执行以下操作:

import requests
import requests.auth as auth
import json

url = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
headers = {'Content-Type' : 'application/x-www-form-urlencoded', 'Host' : 'login.microsoftonline.com' }

r = requests.post(url, data="grant_type=client_credentials&client_id=ID&client_secret=SECRET&scope=https%3A%2F%2Fapi.botframework.com%2F.default")
print(r.content)

jsonAuth = json.loads(r.content)

print(jsonAuth['token_type'] + ' ' + jsonAuth['access_token'])

headers2 = {'Authorization' : 'Bearer ' + jsonAuth['access_token'], 'Content-Type':'application/json' }

url=<service url>+'v3/conversations/'+<conversation id>+'/members'

req = requests.get(url, headers=headers2)

你会收到这样的回复:

[
  {
    "id": <chat id of member1>,
    "name": <name of member1>
  },
  {
    "id": <chat id of member2>,
    "name": <name of member2>
  },
  {
    "id": <chat id of member3>,
    "name": <name of member3>
  }
]

您可以从中获取会员的聊天ID。

现在,您可以继续请求id以开始对话。

要与会员开始对话,在知道会员的聊天ID和姓名后,您应该运行以下内容:

import requests
import requests.auth as auth
import json

url = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
headers = {'Content-Type' : 'application/x-www-form-urlencoded', 'Host' : 'login.microsoftonline.com' }

r = requests.post(url, data="grant_type=client_credentials&client_id=ID&client_secret=SECRET&scope=https%3A%2F%2Fapi.botframework.com%2F.default")
print(r.content)

jsonAuth = json.loads(r.content)

print(jsonAuth['token_type'] + ' ' + jsonAuth['access_token'])

headers2 = {'Authorization' : 'Bearer ' + jsonAuth['access_token'], 'Content-Type':'application/json' }


url = "https://smba.trafficmanager.net/apis/v3/conversations"

user = {}
user['bot'] = {}
user['bot']['id']='7444e829-f753-4f97-95c9-8c33e79087d0'
user['bot']['name']='Jessie'

user['isGroup']=False
user['members']= []
user['members'].append({'id' : <chat id of a member>, 'name' : <name of the member>})

user['topicName'] = 'New Alert!'

jsonRequestBody = json.dumps(user)

print(jsonRequestBody)


req = requests.post(url, headers=headers2, data=jsonRequestBody)
print(req.content)

您可能会收到以下内容:

{
    "id": <new id>
}

注意:在发送个人信息时,<conversation id>恰好与<chat id>相同。

收到<new id>后,您可以使用此类内容最终向对话发送消息:

import requests
import requests.auth as auth
import json

url = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
headers = {'Content-Type' : 'application/x-www-form-urlencoded', 'Host' : 'login.microsoftonline.com' }

r = requests.post(url, data="grant_type=client_credentials&client_id=ID&client_secret=SECRET&scope=https%3A%2F%2Fapi.botframework.com%2F.default")
print(r.content)

jsonAuth = json.loads(r.content)

print(jsonAuth['token_type'] + ' ' + jsonAuth['access_token'])

headers2 = {'Authorization' : 'Bearer ' + jsonAuth['access_token'], 'Content-Type':'application/json' }


url='https://smba.trafficmanager.net/apis/v3/conversations/'+<conversation id>+'/activities/'+<new id>

user = {}
user['conversation'] = {}
user['conversation']['id']=<conversation id>
user['conversation']['name']='New Alert!'

user['from'] = {}
user['from']['id']=<bot id>
user['from']['name']=<name of the bot>

user['recipient'] = {}
user['recipient']['id']=<chat id of the member>
user['recipient']['name']=<name of the member>

user['replyToId']=<new id>
user['text']= 'Some text'
user['type']='message'


jsonRequestBody = json.dumps(user)

print(jsonRequestBody)


req = requests.post(url, headers=headers2, data=jsonRequestBody)
print(req.content)