-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudp-listener.py
executable file
·42 lines (28 loc) · 1.08 KB
/
udp-listener.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
#! /usr/bin/python3
# by FvH, released under Apache License v2.0
# either install 'python3-paho-mqtt' or 'pip3 install paho-mqtt'
import paho.mqtt.client as mqtt
import socket
import threading
import time
import sys
mqtt_server = 'mqtt.vm.nurd.space' # TODO: hostname of MQTT server
topic_prefix = 'GHBot/' # leave this as is
channels = ['nurds'] # TODO: channels to respond to
def listener(client):
try:
s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
s.bind(('0.0.0.0', 9393))
while True:
message, address = s.recvfrom(512)
message = message.decode('ascii')
for channel in channels:
announce_topic = f'{topic_prefix}to/irc/{channel}/privmsg'
client.publish(announce_topic, message)
except Exception as e:
print(e)
client = mqtt.Client(f'{socket.gethostname()}_{sys.argv[0]}', clean_session=False)
client.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
t = threading.Thread(target=listener, args=(client,))
t.start()
client.loop_forever()