Browse code

add sms alert functionality

Gandolf authored on 12/09/2021 02:11:18
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,79 @@
1
+#!/usr/bin/python
2
+
3
+# courtsey ruler for editing script - 80 characters max line length
4
+#2345678901234567890123456789012345678901234567890123456789012345678901234567890
5
+
6
+import telnetlib
7
+
8
+_DEFAULT_HOST = '{your APRS-IS server hostname}'
9
+_DEFAULT_PORT = '{your APRS-IS message port}'
10
+_DEFAULT_SERVER = '{your APRS-IS server name}'
11
+
12
+class smsalert:
13
+
14
+    def __init__(self, callsign, passcode, host=_DEFAULT_HOST, \
15
+      port=_DEFAULT_PORT, server=_DEFAULT_SERVER, debug=False):
16
+        """
17
+        Initialize an instance of this class.
18
+        Parameters:
19
+          callsign - amateur radio callsign of user (must be verified)
20
+          passcode - passcode for verified callsign
21
+          host - domain name or IP address of APRS-IS server
22
+          port - port on which the APRS-IS server receives messages
23
+          server - APRS service name
24
+          debug - set equal to True for debug output
25
+        Returns: nothing
26
+        """
27
+        # Initialize class instance variables.
28
+        self.callsign = callsign
29
+        self.passcode = passcode
30
+        self.host = host
31
+        self.port = port
32
+        self.server = server
33
+        self.debug = debug
34
+    ## end def
35
+
36
+    def sendSMS(self, phone_number, text_message):
37
+        """
38
+        Sends an SMS text message to the provided phone number.
39
+        Parameters:
40
+          phone_number - phone number to which to send the text message
41
+          text_message - text message to be sent to the provided phone number
42
+        Returns: True if successful, False otherwise
43
+        """
44
+        # Establish network connection to APRS-IS server.
45
+        tn = telnetlib.Telnet(self.host, self.port)
46
+        tn.read_until('# aprsc 2.1.8-gf8824e8')
47
+
48
+        # Login and verify passcode accepted.
49
+        tn.write('user ' + self.callsign + ' pass ' + self.passcode + '\n')
50
+        response = tn.read_until(self.server)
51
+        if self.debug:
52
+            print('response: ' + response[2:])
53
+        if not response.find('verified'):
54
+            print('smsalert error: unverified user')
55
+            del tn
56
+            return False
57
+
58
+        # Format and send SMS message to SMS gateway.
59
+        cmd = '%s>%s::SMSGTE:@%s %s' % \
60
+          (self.callsign, self.server, phone_number, text_message)
61
+        if self.debug:
62
+            print('cmd: ' + cmd)
63
+        tn.write(cmd + '\n')
64
+        del tn
65
+        return True
66
+    ## end def
67
+## end class
68
+
69
+def test_smsalert():
70
+    # Initialize a telnet instance.  Default host, port, and server
71
+    # automatically defined if not included in function call.
72
+    sm = smsalert('{your callsign}', '{your passcode}', debug=True)
73
+
74
+    # Send a text message to a phone number.
75
+    sm.sendSMS('{your phone number}', 'Test message send from smsalert.py')
76
+## end def
77
+
78
+if __name__ == '__main__':
79
+    test_smsalert()