-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhipchatclient.py
More file actions
97 lines (70 loc) · 2.66 KB
/
Copy pathhipchatclient.py
File metadata and controls
97 lines (70 loc) · 2.66 KB
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
95
96
97
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hipchat
import config
import datetime
from threading import Thread
class HipChat(hipchat.HipChat):
def __init__(self, queue, token, room_name):
super(HipChat, self).__init__(token=token)
self.queue = queue
self.room_name = room_name
self.room = None
self.running = True
self.notifications = {}
def get_participants(self):
# get room information
try:
room = self.get_extended_room_information(self.room_name)
except Exception, e:
print "Caught %s" % e
return []
return room.get('participants')
def get_extended_room_information(self, name):
room = self.get_room_information(name)
# return extended room information
return self.method('rooms/show',
method='GET',
parameters={'room_id': room.get('room_id')}).get('room')
def get_room_information(self, name):
# if no room information, get it
if self.room is None or (self.room.get('name') != name):
for room in self.get_rooms():
if room.get('name') == name:
self.room = room
return self.room
def get_rooms(self):
# get rooms from hipchat
return self.list_rooms()['rooms']
def send_message(self, username, message):
notify = 0
now = datetime.datetime.now()
if not self.notifications.get(username) or (now - self.notifications.get(username)).total_seconds > config.HIPCHAT_NOTIFICATIONDELAY:
notify = 1
else:
print "Username %s already notified" % username
self.notifications[username] = now
# send message to hipchat
self.method('rooms/message',
method='POST',
parameters={
'room_id': self.room.get('room_id'),
'from': 'Olark',
'message': self.get_formatted_message(username, message),
'notify': notify})
def get_formatted_message(self, username, message):
link = "<a href='https://chat.olark.com'>chat.olark.com</a>"
return "{}: {} -> {}".format(username, message, link)
def worker(self):
# listen the queue to send messages from Olark to Hipchat
while self.running:
(username, message) = self.queue.get()
if username and message:
self.send_message(username, message)
def start(self):
# start the queue listening
t = Thread(target=self.worker)
t.start()
def stop(self):
self.running = False
self.queue.put((None, None))