Skip to content Skip to sidebar Skip to footer

Skype REST API Python Starting Conversation

Just added my bot Jessie to contacts. Now trying to start conversation and nothing is works import requests import requests.auth as auth import json url = 'https://login.microsoft

Solution 1:

It is not clear how you have got the id. As far as I can see, it seems you are apparently using the explicit Skype id (bogdan_danson) of the user in place of user['members'][0]['id'] but I think that's not the id you are supposed to use.

When a webhook is fixed and when a person sends a message to your bot, then a json of this kind will be played:

{
    "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"
}

Now, user['members'][0]['id'] should equal to the <chat id> of the user, instead of his/her explicit Skype id.

To get <chat id> of a new user from a group:

If your bot was added to a group, a json object of this kind must have been received to your webhook:

{
  "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>
}

From this, you can get of the conversation and the chat ids of members of the group right at the instant of time the bot was added to the group.

To get the updated members (chat ids and names), as per API reference, you can do something like this:

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)

And you will receive a response like this:

[
  {
    "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>
  }
]

You can get the chat id of members from this.


Now, you can proceed with requesting for id in order to start a conversation.

To start a conversation with a member, after having known the chat id and name of the member, you are supposed to run something like this:

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)

You may then receive something like this:

{
    "id": <new id>
}

Note: In sending a personal message, <conversation id> happens to be the same as <chat id>.

Once you receive <new id>, you can use something like this to finally send a message to the conversation:

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)

Post a Comment for "Skype REST API Python Starting Conversation"