-
Notifications
You must be signed in to change notification settings - Fork 1
/
GovNotifyMMF.py
189 lines (139 loc) · 5.78 KB
/
GovNotifyMMF.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 18 07:02:31 2021
@author: mark.bailey
"""
import sys
import threading
import mmap
import time as t
from notifications_python_client.notifications import NotificationsAPIClient
singleRun = True
SEPARATOR = "|"
SEPARATOR_SUB = ";"
response = ''
sleepTime = 1
def GOV_UK_notify(arguments):
argSplit = arguments.split(SEPARATOR_SUB)
method = argSplit[0]
api_key = argSplit[1]
template_ID = argSplit[2]
try:
if method == 'SMS':
notifications_client = NotificationsAPIClient(api_key) # API_key from GOV.uk Notify
response = notifications_client.send_sms_notification(
template_id= template_ID, # This is the SMS template ID on GOV.uk Notify
phone_number= argSplit[3], # Mobile number in format 01234 567890
personalisation={
'message': argSplit[4], # The message to send via SMS
}
)
elif method == 'email':
notifications_client = NotificationsAPIClient(api_key) # API_key from GOV.uk Notify
response = notifications_client.send_email_notification(
template_id= template_ID, # This is the email template ID on GOV.uk Notify
email_address= argSplit[3], # email address
personalisation={
'subject': argSplit[4],
'message': argSplit[5],
}
)
elif method == 'letter':
notifications_client = NotificationsAPIClient(api_key) # API_key from GOV.uk Notify
addressLines = argSplit[3].split("//")
personalisationT={
'from' : argSplit[4],
'heading' : argSplit[5],
'body' : argSplit[6],
}
for index, line in enumerate(addressLines):
lineNr = index + 1
personalisationT['address_line_' + str(lineNr)] = line
response = notifications_client.send_letter_notification(template_id=template_ID, personalisation = personalisationT)
else:
return "wrong method"
except:
return "fail"
else:
return "pass - %s" %(response)
return "error"
def sleepTimeF(argument):
global sleepTime
sleepTime = int(argument)
#print('Sleep timer set to: %s' %(argument))
return ' '
class MemoryMappedFile_IPC:
def __init__(self, MMFName):
self.MMFName = MMFName
self.MMFInstance = mmap.mmap(0, 1000000, self.MMFName, mmap.ACCESS_WRITE)
def read(self):
self.MMFInstance.seek(0) #Move back to beginning of file
received = str(self.MMFInstance.read())
if received.find("<") == 2:
endMarker = received.find(">")
if endMarker != -1:
return received[3:endMarker]
else:
return -1
def write(self, func, argument = ""):
global SEPARATOR
func = func.replace("<", "")
func = func.replace(">", "")
func = func.replace("|", "")
argument = argument.replace("<", "less than")
argument = argument.replace(">", "more than")
argument = argument.replace("|", "pipe")
if argument == "":
buffer = "<" + func + ">"
else:
buffer = "<" + func + SEPARATOR + argument + ">"
self.MMFInstance.seek(0) #Move back to beginning of file
self.MMFInstance.write(bytes(buffer, 'UTF-8'))
class mainClass(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
mainThread()
def mainThread():
global SEPARATOR, count_vect, clf, pythonFolder
AHK_Messages = MemoryMappedFile_IPC("AHK_2_Python_IPC")
while True:
MMFRead = AHK_Messages.read()
try:
MMFSplit = MMFRead.split(SEPARATOR)
functionCall = MMFSplit[0]
# Cannot assign MMFSplit[1] here as causes issues, done later!
except:
None
else:
if "close" in functionCall:
AHK_Messages.write("SPRead")
break
elif functionCall != "SPRead" and functionCall != "wait":
HandleError = False # Helps to show up errors with the dispacher function calls
argument = MMFSplit[1]
dispatcher = {'GOV_UK_notify' : GOV_UK_notify, 'sleepTimeF' : sleepTimeF}
if functionCall in dispatcher:
if HandleError == True:
try:
returnResult = dispatcher[functionCall](argument)
except:
AHK_Messages.write("SPRead", 'error')
else:
AHK_Messages.write("SPRead", returnResult)
else:
returnResult = dispatcher[functionCall](argument)
AHK_Messages.write("SPRead", returnResult)
else:
errorMessage = 'Function name \'%s\' was not found in python script' %(functionCall)
AHK_Messages.write("SPRead", errorMessage)
if singleRun:
break
if singleRun:
t.sleep(0.1)
else:
t.sleep(sleepTime)
sys.exit()
if __name__ == '__main__':
main = mainClass()
main.start()