-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmqtt-topics.py
More file actions
162 lines (105 loc) · 3.72 KB
/
mqtt-topics.py
File metadata and controls
162 lines (105 loc) · 3.72 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /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 sqlite3
import threading
import time
mqtt_server = 'mqtt.vm.nurd.space'
topic_prefix = 'GHBot/'
channels = ['nurdbottest', 'nurds', 'test', 'nurdsbofh']
prefix = '!'
db_file = 'mqtt.db'
con2 = None
con1 = sqlite3.connect(db_file)
cur = con1.cursor()
try:
cur.execute('CREATE TABLE mqtt(topic TEXT NOT NULL PRIMARY KEY, value TEXT NOT NULL)')
except sqlite3.OperationalError as oe:
# should be "table already exists"
pass
cur.close()
cur = con1.cursor()
cur.execute('PRAGMA journal_mode=wal')
cur.close()
con1.commit()
def announce_commands(client):
target_topic = f'{topic_prefix}to/bot/register'
# client.publish(target_topic, 'cmd=btc|descr=BTC koers')
def cmd_janee(client, response_topic):
client.publish(response_topic, random.choice(['ja', 'nee', 'ja', 'nein']))
def get_topic(topic):
cur = con1.cursor()
cur.execute('SELECT value FROM mqtt WHERE topic=?', (topic,))
row = cur.fetchone()
cur.close()
return row[0]
def on_message(client, userdata, message):
global choices
global prefix
text = message.payload.decode('utf-8')
topic = message.topic[len(topic_prefix):]
if topic == 'from/bot/command' and text == 'register':
announce_commands(client)
return
if topic == 'from/bot/parameter/prefix':
prefix = text
return
if len(text) == 0:
return
if text[0] != prefix:
return
parts = topic.split('/')
channel = parts[2] if len(parts) >= 3 else 'nurds'
nick = parts[3] if len(parts) >= 4 else 'jemoeder'
parts = text.split(' ')
command = parts[0][1:]
value = parts[1] if len(parts) >= 2 else None
value_all = parts[1:] if len(parts) >= 2 else None
# print(channel, nick, command, value)
if channel in channels or (len(channel) >= 1 and channel[0] == '\\'):
command = text[1:].split(' ')[0]
response_topic = f'{topic_prefix}to/irc/{channel}/privmsg'
# if command == 'btc':
# value = get_topic('vanheusden/bitcoin/bitstamp_usd')
# client.publish(response_topic, f'btc: {value}')
def on_connect(client, userdata, flags, rc):
client.subscribe(f'{topic_prefix}from/irc/#')
client.subscribe(f'{topic_prefix}from/bot/command')
def on_connect_all(client, userdata, flags, rc):
client.subscribe(f'#')
def on_message_all(client, userdata, message):
global con2
text = message.payload.decode('utf-8')
topic = message.topic
if topic_prefix in topic:
return
cur = con2.cursor()
cur.execute('INSERT INTO mqtt(topic, value) VALUES(?, ?) ON CONFLICT(topic) DO UPDATE SET value=?', (topic, text, text))
cur.close()
con2.commit()
def announce_thread(client):
while True:
try:
announce_commands(client)
time.sleep(4.1)
except Exception as e:
print(f'Failed to announce: {e}')
def mqtt_all_thread():
global con2
client2 = mqtt.Client(sys.argv[0], clean_session=False)
client2.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
client2.on_message = on_message_all
client2.on_connect = on_connect_all
con2 = sqlite3.connect(db_file)
while True:
client2.loop_forever()
client = mqtt.Client(sys.argv[0], clean_session=False)
client.on_message = on_message
client.on_connect = on_connect
client.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
t1 = threading.Thread(target=announce_thread, args=(client,))
t1.start()
t2 = threading.Thread(target=mqtt_all_thread, args=())
t2.start()
client.loop_forever()