You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+99-9
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,91 @@ Python package to build your own Signal bots. To run the the bot you need to sta
4
4
5
5
## Getting Started
6
6
7
-
Please see https://github.com/filipre/signalbot-example for an example how to use the package and how to build a simple bot.
7
+
Below you can find a minimal example on how to use the package. There is also a bigger example in the `example` folder.
8
+
9
+
```python
10
+
import os
11
+
from signalbot import SignalBot, Command, Context
12
+
from commands import PingCommand
13
+
14
+
15
+
classPingCommand(Command):
16
+
asyncdefhandle(self, c: Context):
17
+
if c.message.text =="Ping":
18
+
await c.send("Pong")
19
+
20
+
21
+
if__name__=="__main__":
22
+
bot = SignalBot({
23
+
"signal_service": os.environ["SIGNAL_SERVICE"],
24
+
"phone_number": os.environ["PHONE_NUMBER"]
25
+
})
26
+
bot.register(PingCommand()) # all contacts and groups
27
+
bot.start()
28
+
```
29
+
30
+
Please check out https://github.com/bbernhard/signal-cli-rest-api#getting-started to learn about [signal-cli-rest-api](https://github.com/bbernhard/signal-cli-rest-api) and [signal-cli](https://github.com/AsamK/signal-cli). A good first step is to make the example above work.
31
+
32
+
1. Run signal-cli-rest-api in `normal` mode first.
2. Open http://127.0.0.1:8080/v1/qrcodelink?device_name=local to link your account with the signal-cli-rest-api server
40
+
41
+
3. In your Signal app, open settings and scan the QR code. The server can now receive and send messages. The access key will be stored in `$(PWD)/signal-cli-config`.
5. The logs should show something like this. You can also confirm that the server is running in the correct mode by visiting http://127.0.0.1:8080/v1/about.
51
+
```
52
+
...
53
+
time="2022-03-07T13:02:22Z" level=info msg="Found number +491234567890 and added it to jsonrpc2.yml"
54
+
...
55
+
time="2022-03-07T13:02:24Z" level=info msg="Started Signal Messenger REST API"
56
+
```
57
+
58
+
6. Use the following snippet to get a group's `id`:
59
+
```bash
60
+
curl -X GET 'http://127.0.0.1:8080/v1/groups/+49123456789'| python -m json.tool
61
+
```
62
+
63
+
7. Install `signalbot` and start your python script. You need to pass following environment variables to make the example run:
64
+
-`SIGNAL_SERVICE`: Address of the signal service without protocol, e.g. `127.0.0.1:8080`
65
+
-`PHONE_NUMBER`: Phone number of the bot, e.g. `+49123456789`
66
+
67
+
```bash
68
+
export SIGNAL_SERVICE="127.0.0.1"
69
+
export PHONE_NUMBER="+49123456789"
70
+
pip install signalbot
71
+
python bot.py
72
+
```
73
+
74
+
8. The logs should indicate that one "producer" and three "consumers" have started. The producer checks for new messages sent to the linked account using a web socket connection. It creates a task for every registered command and the consumers work off the tasks. In case you are working with many blocking function calls, you may need to adjust the number of consumers such that the bot stays reactive.
75
+
```
76
+
INFO:root:[Bot] Producer #1 started
77
+
INFO:root:[Bot] Consumer #1 started
78
+
INFO:root:[Bot] Consumer #2 started
79
+
INFO:root:[Bot] Consumer #3 started
80
+
```
81
+
82
+
9. Send the message `Ping` (case sensitive) to the group that the bot is listening to. The bot (i.e. the linked account) should respond with a `Pong`. Confirm that the bot received a raw message, that the consumer worked on the message and that a new message has been sent.
INFO:root:[Bot] Consumer #2 got new job in 0.00046 seconds
86
+
INFO:root:[Bot] Consumer #2 got new job in 0.00079 seconds
87
+
INFO:root:[Bot] Consumer #2 got new job in 0.00093 seconds
88
+
INFO:root:[Bot] Consumer #2 got new job in 0.00106 seconds
89
+
INFO:root:[Bot] New message 1646000000000 sent:
90
+
Pong
91
+
```
8
92
9
93
## Classes and API
10
94
@@ -14,11 +98,11 @@ The package provides methods to easily listen for incoming messages and respondi
14
98
15
99
### Signalbot
16
100
17
-
-`bot.listen(group_id, internal_id)`: Listen for messages in a group chat. `group_id` must be prefixed with `group.`
18
-
-`bot.listen(phone_number)`: Listen for messages in a user chat.
19
-
-`bot.register(command)`: Register a new command
101
+
-`bot.register(command, contacts=True, groups=True)`: Register a new command, listen in all contacts and groups, same as `bot.register(command)`
102
+
-`bot.register(command, contacts=False, groups=["Hello World"])`: Only listen in the "Hello World" group
103
+
-`bot.register(command, contacts=["+49123456789"], groups=False)`: Only respond to one contact
20
104
-`bot.start()`: Start the bot
21
-
-`bot.send(receiver, text, listen=False)`: Send a new message
105
+
-`bot.send(receiver, text)`: Send a new message
22
106
-`bot.react(message, emoji)`: React to a message
23
107
-`bot.start_typing(receiver)`: Start typing
24
108
-`bot.stop_typing(receiver)`: Stop typing
@@ -31,10 +115,12 @@ To implement your own commands, you need to inherent `Command` and overwrite fol
31
115
32
116
-`setup(self)`: Start any task that requires to send messages already, optional
33
117
-`describe(self)`: String to describe your command, optional
34
-
-`handle(self, c: Context)`: Handle an incoming message. By default, any command will read any incoming message. `Context` can be used to easily reply (`c.send(text)`), react (`c.react(emoji)`) and to type in a group (`c.start_typing()` and `c.stop_typing()`). You can use the `@triggered` decorator to listen for specific commands or you can inspect `c.message.text`.
118
+
-`handle(self, c: Context)`: Handle an incoming message. By default, any command will read any incoming message. `Context` can be used to easily send (`c.send(text)`), reply (`c.reply(text)`), react (`c.react(emoji)`) and to type in a group (`c.start_typing()` and `c.stop_typing()`). You can use the `@triggered` decorator to listen for specific commands or you can inspect `c.message.text`.
35
119
36
120
### Unit Testing
37
121
122
+
*Note: deprecated, I want to switch to pytest eventually*
123
+
38
124
In many cases, we can mock receiving and sending messages to speed up development time. To do so, you can use `signalbot.utils.ChatTestCase` which sets up a "skeleton" bot. Then, you can send messages using the `@chat` decorator in `signalbot.utils` like this:
39
125
```python
40
126
classPingChatTest(ChatTestCase):
@@ -79,8 +165,12 @@ There are a few other related projects similar to this one. You may want to chec
0 commit comments