-
Notifications
You must be signed in to change notification settings - Fork 3
/
yj_signalbot_regular.py
112 lines (95 loc) · 3.49 KB
/
yj_signalbot_regular.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# the main script using a SQLite DB to store subscribers into a Signal Channel
# 2020 Yves Jeanrenaud for PocketPC.ch
import os,sys
dbFilename= os.path.abspath(os.path.dirname(sys.argv[0]))+'/signal.db'
msg ="Unbekannter Befehl.\nDies ist der Channel von PocketPC.ch\n-----------------------------------------------\nSende START zum Anmelden, STOPP zum Abmelden."
def yjDbHandler (source,timestamp,command):
global msg
import sqlite3
from pprint import pprint
import arrow
# db file is now existing
# connect to db
try:
connection = sqlite3.connect(dbFilename, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
except Error as e:
pprint(e)
# create db cursor
cursor = connection.cursor()
if command == "subscribe":
# create table data
strFormatedDateTime=arrow.get(timestamp).format('YYYY-MM-DD HH:mm:ss ZZ')
#check for exisiting number is done by sql
sql = "INSERT INTO subscribers VALUES(\'"+str(source)+"\', "+str(timestamp)+",0 )"
try:
cursor.execute(sql)
except sqlite3.IntegrityError:
#already existing
msg="bereits angemeldet"
else:
connection.commit()
#now set the formated data right in an extra commit
sql="UPDATE subscribers SET subsDateTime = \'"+strFormatedDateTime+"\' WHERE phone = \'"+str(source)+"\'"
cursor.execute(sql)
connection.commit()
msg="erfolgreich angemeldet"
elif command == "unsubscribe":
#check if there is one to unsubscribe goes by SQL
sql = "DELETE FROM subscribers WHERE phone = \'"+str(source)+"\'"
try:
cursor.execute(sql)
except sqlite3.IntegrityError:
#not there
msg="nicht angemeldet?"
else:
cursor.execute(sql)
msg="erfolgreich abgemeldet"
connection.commit()
connection.close()
return
def msgRcv (timestamp, source, groupID, message, attachments):
global msg
print(str(message).lower()+" from " + str(source))
if str(message).lower()=="start" or str(message).lower() == "anmelden" or str(message).lower() == "subscribe" or str(message).lower() == "start":
print ("subscribe "+source +"!")
#go and subcribe that number
yjDbHandler(source,timestamp,"subscribe")
elif str(message).lower()=="stop" or str(message).lower() == "stopp" or str(message).lower() == "abmelden" or str(message).lower() == "unsubscribe":
print ("unsubscribe "+source +"!")
#go and unsubscribe
yjDbHandler(source,timestamp,"unsubscribe")
else:
#reply with help message
msg="Unbekannter Befehl.\nDies ist der Channel von PocketPC.ch\n-----------------------------------------------\nSende START zum Anmelden, STOPP zum Abmelden."
#send the message back!
print (msg)
signal.sendMessage(msg, [], [str(source)])
return
from pydbus import SystemBus
from gi.repository import GLib
bus = SystemBus()
loop = GLib.MainLoop()
signal = bus.get('org.asamk.Signal')
signal.onMessageReceived = msgRcv
import os, sys
if os.path.exists(dbFilename):
print("Datenbank-Datei \""+dbFilename+"\" ist vorhanden")
else:
print("Datenbank-Datei \""+dbFilename+"\" NICHT vorhanden")
# connetion to SQlite
connection = sqlite3.connect(dbFilename, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
# Db cursor
cursor = connection.cursor()
# initially create db
sql = "CREATE TABLE subscribers(" \
"phone TEXT NOT NULL PRIMARY KEY, " \
"subsDatestamp INTEGER, " \
"subsDateTime TEXT )"
cursor.execute(sql)
connection.commit()
connection.close()
print("Datenbank-Datei \""+dbFilename+"\" erstellt")
## now enter the loop
print ("Warte auf Nachrichten, schweigend...")
loop.run()