目前尚不清楚您是如何获得该 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>
用户的,而不是他/她的明确 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。
要获取更新的成员(聊天 ID 和名称),根据API 参考,您可以执行以下操作:
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)