Skype REST API python 开始对话

Skype REST API python starting conversation

刚刚将我的机器人 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还没发布,刚试过

我想开始与用户对话,然后read/answer与用户聊天或群组

不清楚你是怎么得到这个id的。据我所知,您显然是在使用用户的显式 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>,而不是 his/her 明确的 Skype id。

要从组中获取 <chat id> 个新用户:

如果您的 bot 已添加到群组,则您的 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)