-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.py
47 lines (37 loc) · 1.18 KB
/
notify.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
#!/usr/bin/env python
"""Create a notification and send it to RabbitMQ"""
# Licensed under the MIT License, see LICENSE for details
import json
import pika
from settings import MQ_HOST, MQ_PORT, MQ_USER, MQ_PASSWORD, QUEUE
def notify(group, message):
"""
Queue a message in RabbitMQ
Parameters
----------
group : str
name of the group to which the message is addressed
message : str
the message
"""
# Create a RabbitMQ connection
credentials = pika.credentials.PlainCredentials(MQ_USER, MQ_PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=MQ_HOST,
port=MQ_PORT,
credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue=QUEUE)
# Create a JSON object and send it to RabbitMQ
data = {}
data['group'] = group
data['message'] = message
channel.basic_publish(exchange='', routing_key=QUEUE,
body=json.dumps(data))
connection.close()
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print("Usage: notify.py [group] [message]")
else:
notify(sys.argv[1], sys.argv[2])