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
|
pysignald
=======
[![pipeline status](https://gitlab.com/stavros/pysignald/badges/master/pipeline.svg)](https://gitlab.com/stavros/pysignald/commits/master)
pysignald is a Python client for the excellent [signald](https://git.callpipe.com/finn/signald) project, which in turn
is a command-line client for the Signal messaging service.
pysignald allows you to programmatically send and receive messages to Signal.
Installation
------------
You can install pysignald with pip:
```
$ pip install pysignald
```
Running
-------
Just make sure you have signald installed. Here's an example of how to use pysignald:
```python
from signald import Signal
s = Signal("+1234567890")
# If you haven't registered/verified signald, do that first:
s.register(voice=False)
s.verify("sms code")
s.send_message("+1098765432", "Hello there!")
for message in s.receive_messages():
print(message)
```
You can also use the chat decorator interface:
```python
from signald import Signal
s = Signal("+1234567890")
@s.chat_handler("hello there", order=10) # This is case-insensitive.
def hello_there(message, match):
# Returning `False` as the first argument will cause matching to continue
# after this handler runs.
stop = False
reply = "Hello there!"
return stop, reply
# Matching is case-insensitive. The `order` argument signifies when
# the handler will try to match (default is 100), and functions get sorted
# by order of declaration secondly.
@s.chat_handler("hello", order=10)
def hello(message, match):
# This will match on "hello there" as well because of the "stop" return code in
# the function above. Both replies will be sent.
return "Hello!"
@s.chat_handler(re.compile("my name is (.*)")) # This is case-sensitive.
def name(message, match):
return "Hello %s." % match.group(1)
@s.chat_handler("")
def catch_all(message, match):
# This will only be sent if nothing else matches, because matching
# stops by default on the first function that matches.
return "I don't know what you said."
s.run_chat()
```
Various
-------
pysignald also supports different socket paths:
```python
s = Signal("+1234567890", socket_path="/var/some/other/socket.sock")
```
It supports TCP sockets too, if you run a proxy. For example, you can proxy signald's UNIX socket over TCP with socat:
```bash
$ socat -d -d TCP4-LISTEN:15432,fork UNIX-CONNECT:/var/run/signald/signald.sock
```
Then in pysignald:
```python
s = Signal("+1234567890", socket_path=("your.serveri.ip", 15432))
```
|