-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo.py
executable file
·95 lines (79 loc) · 2.6 KB
/
demo.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env python
"""Post message to chat with specific topic.
"""
import requests
import json
# api location
HOST = "https://fleep.io"
# auth info
FLEEP_ID = "my.test.bot" # email can be used as well, i.e. "my.test.bot@gmail.com"
PSW = "12345678"
# chat topic to find
TOPIC = "Api send test"
# message to post
MESSAGE = "Hello world!"
# members for new chat
MEMBERS = "scott.bluemount@fleep.ee, paul.greenlamp@fleep.ee, mary.whitecloud@fleep.ee, julie.roseplum@fleep.ee"
# leave chat after create?
LEAVE_CHAT = False
# login
r = requests.post(HOST + "/api/account/login",
headers = {"Content-Type": "application/json"},
data = json.dumps({
"email": FLEEP_ID,
"password": PSW}))
r.raise_for_status()
TICKET = r.json()["ticket"]
TOKEN = r.cookies["token_id"]
# initial sync via poll, find chat
event_horizon = 0
CONV_ID = None
while 1:
r = requests.post(HOST + "/api/account/poll",
cookies = {"token_id": TOKEN},
headers = {"Content-Type": "application/json"},
data = json.dumps({
"event_horizon": event_horizon,
"wait": False,
"ticket": TICKET}))
r.raise_for_status()
# find test conversation
res = r.json()
for srec in res['stream']:
if srec['mk_rec_type'] == 'conv':
if srec['topic'] == TOPIC:
CONV_ID = srec['conversation_id']
break
# fetch more data?
if res['event_horizon'] != event_horizon:
event_horizon = res['event_horizon']
else:
break
if CONV_ID:
# send message
r = requests.post(HOST + "/api/message/send/" + CONV_ID,
cookies = {"token_id": TOKEN},
headers = {"Content-Type": "application/json"},
data = json.dumps({
"message": MESSAGE,
"ticket": TICKET}))
r.raise_for_status()
else:
# no chat was found, create & post
r = requests.post(HOST + "/api/conversation/create",
cookies = {"token_id": TOKEN},
headers = {"Content-Type": "application/json"},
data = json.dumps({
"topic": TOPIC,
"emails": MEMBERS,
"message": MESSAGE,
"ticket": TICKET}))
r.raise_for_status()
if LEAVE_CHAT:
res = r.json()
r = requests.post(HOST + "/api/conversation/leave/" + res.get('header').get('conversation_id'),
cookies={"token_id": TOKEN},
headers={"Content-Type": "application/json"},
data=json.dumps({"ticket": TICKET}))
r.raise_for_status()
print 'Posted'