-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbash_skype
83 lines (67 loc) · 2.59 KB
/
bash_skype
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
# Most of the below utilites depends on the "jq" tool for parsing the
# See https://stedolan.github.io/jq/ for installing jq tool.
# JSON reponse from JIRA rest API. Below environment variables need to
# be configured for the tool to function.
# SKYPE_BOT_ID - Skype bot id from Azure
# SKYPE_BOT_PASSWORD - Skype bot password from Azure
# SKYPE_BOT_AUTH_URL - Skype bot login url
# SKYPE_BOT_API_URL - Skype bot service url
# SKYPE_AUTH_TOKEN_DB - File to cache auth token for Skype bot
# configure bot credentials here.
export SKYPE_BOT_ID=
export SKYPE_BOT_PASSWORD=
export SKYPE_BOT_AUTH_URL="https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
export SKYPE_BOT_API_URL="https://smba.trafficmanager.net/apis"
export SKYPE_AUTH_TOKEN_DB="/tmp/.skype_get_auth_token.db"
skype_send_chat() {
local conversation_id message token OPTIND choice
while getopts 'c:m:h' choice
do
case $choice in
c) conversation_id=$OPTARG ;;
m) message=$OPTARG ;;
h) echo "Usage: skype_send_chat -c conversation_id -m message [-h]"; return
esac
done
if [[ -z "$conversation_id" || -z "$message" ]]; then
skype_send_chat -h
return
fi
if ! [ -e $SKYPE_AUTH_TOKEN_DB ]; then
_skype_get_auth_token > $SKYPE_AUTH_TOKEN_DB
fi
token=`cat $SKYPE_AUTH_TOKEN_DB`
response=`_skype_send_message "$conversation_id" "$message" "$token"`
response_code=`echo "$response" | awk 'NR==1{print $2}'`
if [ $response_code == "401" ]; then #Unautohrized
_skype_get_auth_token > $SKYPE_AUTH_TOKEN_DB
token=`cat $SKYPE_AUTH_TOKEN_DB`
response=`_skype_send_message "$conversation_id" "$message" "$token"`
response_code=`echo "$response" | awk 'NR==1{print $2}'`
fi
if [ $response_code == "201" ]; then
echo "Skype message has been sent successfully."
else
echo "Something went wrong, the message is not sent. reponse = $response"
fi
}
_skype_get_auth_token() {
curl -s -X POST $SKYPE_BOT_AUTH_URL -H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode "client_id=$SKYPE_BOT_ID" \
--data-urlencode "client_secret=$SKYPE_BOT_PASSWORD" \
--data-urlencode 'scope=https://api.botframework.com/.default' | jq -r '.access_token'
}
_skype_send_message() {
local message conversation_id toekn url payload token
conversation_id="$1"
message="$2"
token="$3"
url="$SKYPE_BOT_API_URL/v3/conversations/$conversation_id/activities"
payload=$(jq -n \
--arg t "message" \
--arg m "$message" \
'{type: $t, text: $m}')
curl -s -i -X POST -H "Authorization: Bearer $token" -H "Content-Type: application/json" "$url" -d "$payload"
}