-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathskype_sender.py
44 lines (37 loc) · 1.5 KB
/
skype_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
import requests
import json
from config.cfg import SKYPE_CLIENT_ID, SKYPE_CLIENT_SECRET
CLIENT_ID = SKYPE_CLIENT_ID
CLIENT_SECRET = SKYPE_CLIENT_SECRET
def get_access_token(client_id, client_secret):
payload = {
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://api.botframework.com/.default',
'grant_type': 'client_credentials'
}
r = requests.post("https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token", data=payload,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
try:
response = json.loads(r.text)
return response['access_token']
except:
return False
def send_message(token, skype_id, message, type):
if type == 'direct':
URL = "https://apis.skype.com/v2/conversations/8:{}/activities".format(skype_id)
if type == 'chat':
URL = "https://apis.skype.com/v2/conversations/19:{}/activities".format(skype_id)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer %s' % token
}
data = json.dumps(dict(message=dict(content=message))).encode()
r = requests.post(URL, data=data, headers=headers)
return r.text, r.status_code
def main(message, skype_id):
TOKEN = get_access_token(CLIENT_ID, CLIENT_SECRET)
if TOKEN:
conversation_type = 'chat' if 'thread.skype' in skype_id else 'direct'
return send_message(TOKEN, skype_id, message, conversation_type)