-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipchat_block.py
88 lines (73 loc) · 2.86 KB
/
hipchat_block.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
from nio import TerminatorBlock
from nio.command import command
from nio.properties import BoolProperty, SelectProperty, \
StringProperty, Property, VersionProperty
from nio.block.mixins.persistence.persistence import Persistence
from hipchat import HipChat as HC
from enum import Enum
class Color(Enum):
NONE = ''
YELLOW = "yellow"
RED = "red"
GREEN = "green"
PURPLE = "purple"
GRAY = "gray"
RANDOM = "random"
@command('list_rooms')
@command('list_users')
class HipChat(Persistence, TerminatorBlock):
""" A block for sending messages to a HipChat room.
Properties:
token (str): Auth token (distributed by account admin)
message (expr): Message contents.
room_name (str): The plaintext name of the target HipChat room.
sender (expr): Sender name to attach to the message.
message_color (enum): The color of the message.
notify (bool): Whether or not to send a notification to the members
of the room when the message arrives.
"""
version = VersionProperty("1.0.1")
token = StringProperty(title="API Token", default="[[HIPCHAT_TOKEN]]")
message = Property(title="Message contents", default='')
room_name = StringProperty(title="Room Name", default='')
sender = Property(title="Sender Name", default='')
message_color = SelectProperty(
Color, title="Message Color", default=Color.NONE)
notify = BoolProperty(title="Notify Users in Room", default=False)
def __init__(self):
super().__init__()
self.hipster = None
self.room_id = None
def configure(self, context):
super().configure(context)
self.hipster = HC(token=self.token())
if self.room_id is None:
room = self.hipster.find_room(self.room_name())
if room is None:
self.logger.error(
"Hipchat room '{}' does not exist".format(self.room_name())
)
else:
self.room_id = room.get('room_id')
def start(self):
super().start()
def process_signals(self, signals):
for signal in signals:
msg = self.message(signal)
sender = self.sender(signal)
try:
self.hipster.message_room(self.room_id, sender, msg,
color=self.message_color().value,
notify=self.notify())
except Exception as e:
self.logger.error(
"Failed to message HipChat room {}: {}".format(
self.room_name(), str(e))
)
def persisted_values(self):
"""Persist room_name using block mixin."""
return ["room_id"]
def list_rooms(self):
return self.hipster.list_rooms()
def list_users(self):
return self.hipster.list_users()