# Signal Bot Framework
Python package to build your own Signal bots. To run the the bot you need to start the [signal-cli-rest-api](https://github.com/bbernhard/signal-cli-rest-api) service and link your device with it. Please refer to that project for more details. The API server must run in `json-rpc` mode.
## Getting Started
Below you can find a minimal example on how to use the package. There is also a bigger example in the `example` folder.
```python
import os
from signalbot import SignalBot, Command, Context
from commands import PingCommand
class PingCommand(Command):
async def handle(self, c: Context):
if c.message.text == "Ping":
await c.send("Pong")
if __name__ == "__main__":
bot = SignalBot({
"signal_service": os.environ["SIGNAL_SERVICE"],
"phone_number": os.environ["PHONE_NUMBER"]
})
bot.register(PingCommand()) # all contacts and groups
bot.start()
```
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.
1. Run signal-cli-rest-api in `normal` mode first.
```bash
docker run -p 8080:8080 \
-v $(PWD)/signal-cli-config:/home/.local/share/signal-cli \
-e 'MODE=normal' bbernhard/signal-cli-rest-api:0.57
```
2. Open http://127.0.0.1:8080/v1/qrcodelink?device_name=local to link your account with the signal-cli-rest-api server
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`.
4. Restart the server in `json-rpc` mode.
```bash
docker run -p 8080:8080 \
-v $(PWD)/signal-cli-config:/home/.local/share/signal-cli \
-e 'MODE=json-rpc' bbernhard/signal-cli-rest-api:0.57
```
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.
```
...
time="2022-03-07T13:02:22Z" level=info msg="Found number +491234567890 and added it to jsonrpc2.yml"
...
time="2022-03-07T13:02:24Z" level=info msg="Started Signal Messenger REST API"
```
6. Use the following snippet to get a group's `id`:
```bash
curl -X GET 'http://127.0.0.1:8080/v1/groups/+49123456789' | python -m json.tool
```
7. Install `signalbot` and start your python script. You need to pass following environment variables to make the example run:
- `SIGNAL_SERVICE`: Address of the signal service without protocol, e.g. `127.0.0.1:8080`
- `PHONE_NUMBER`: Phone number of the bot, e.g. `+49123456789`
```bash
export SIGNAL_SERVICE="127.0.0.1"
export PHONE_NUMBER="+49123456789"
pip install signalbot
python bot.py
```
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.
```
INFO:root:[Bot] Producer #1 started
INFO:root:[Bot] Consumer #1 started
INFO:root:[Bot] Consumer #2 started
INFO:root:[Bot] Consumer #3 started
```
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:[Raw Message] {"envelope":{"source":"+49123456789","sourceNumber":"+49123456789","sourceUuid":"fghjkl-asdf-asdf-asdf-dfghjkl","sourceName":"René","sourceDevice":3,"timestamp":1646000000000,"syncMessage":{"sentMessage":{"destination":null,"destinationNumber":null,"destinationUuid":null,"timestamp":1646000000000,"message":"Pong","expiresInSeconds":0,"viewOnce":false,"groupInfo":{"groupId":"asdasdfweasdfsdfcvbnmfghjkl=","type":"DELIVER"}}}},"account":"+49123456789","subscription":0}
INFO:root:[Bot] Consumer #2 got new job in 0.00046 seconds
INFO:root:[Bot] Consumer #2 got new job in 0.00079 seconds
INFO:root:[Bot] Consumer #2 got new job in 0.00093 seconds
INFO:root:[Bot] Consumer #2 got new job in 0.00106 seconds
INFO:root:[Bot] New message 1646000000000 sent:
Pong
```
## Classes and API
*Documentation work in progress. Feel free to open an issue for questions.*
The package provides methods to easily listen for incoming messages and responding or reacting on them. It also provides a class to develop new commands which then can be registered within the bot.
### Signalbot
- `bot.register(command, contacts=True, groups=True)`: Register a new command, listen in all contacts and groups, same as `bot.register(command)`
- `bot.register(command, contacts=False, groups=["Hello World"])`: Only listen in the "Hello World" group
- `bot.register(command, contacts=["+49123456789"], groups=False)`: Only respond to one contact
- `bot.start()`: Start the bot
- `bot.send(receiver, text)`: Send a new message
- `bot.react(message, emoji)`: React to a message
- `bot.start_typing(receiver)`: Start typing
- `bot.stop_typing(receiver)`: Stop typing
- `bot.scheduler`: APScheduler > AsyncIOScheduler, see [here](https://apscheduler.readthedocs.io/en/3.x/modules/schedulers/asyncio.html?highlight=AsyncIOScheduler#apscheduler.schedulers.asyncio.AsyncIOScheduler)
- `bot.storage`: In-memory or Redis stroage, see `storage.py`
### Command
To implement your own commands, you need to inherent `Command` and overwrite following methods:
- `setup(self)`: Start any task that requires to send messages already, optional
- `describe(self)`: String to describe your command, optional
- `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`.
### Unit Testing
*Note: deprecated, I want to switch to pytest eventually*
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:
```python
class PingChatTest(ChatTestCase):
def setUp(self):
# initialize self.singal_bot
super().setUp()
# all that is left to do is to register the commands that you want to test
self.signal_bot.register(PingCommand())
@chat("ping", "ping")
async def test_ping(self, query, replies, reactions):
self.assertEqual(replies.call_count, 2)
for recipient, message in replies.results():
self.assertEqual(recipient, ChatTestCase.group_secret)
self.assertEqual(message, "pong")
```
In `signalbot.utils`, check out `ReceiveMessagesMock`, `SendMessagesMock` and `ReactMessageMock` to learn more about their API.
## Troubleshooting
- Check that you linked your account successfully
- Is the API server running in `json-rpc` mode?
- Can you receive messages using `wscat` (websockets) and send messages using `curl` (http)?
- Do you see incoming messages in the API logs?
- Do you see the "raw" messages in the bot's logs?
- Do you see "consumers" picking up jobs and handling incoming messages?
- Do you see the response in the bot's logs?
## Local development and package
*Section work in progress. Feel free to open an issue for questions.*
```bash
poetry install
poetry run pre-commit install
```
## Other Projects
There are a few other related projects similar to this one. You may want to check them out and see if it fits your needs.
|Project|Description|Language|
|-------|-----------|--------|
|https://github.com/lwesterhof/semaphore|Bot Framework|Python|
|https://git.sr.ht/~nicoco/aiosignald|signald Library / Bot Framework|Python|
|https://gitlab.com/stavros/pysignald/|signald Library / Bot Framework|Python|
|https://gitlab.com/signald/signald-go|signald Library|Go|
|https://github.com/signal-bot/signal-bot|Bot Framework using Signal CLI|Python|
|https://github.com/bbernhard/signal-cli-rest-api|REST API Wrapper for Signal CLI|Go|
|https://github.com/bbernhard/pysignalclirestapi|Python Wrapper for REST API|Python|
|https://github.com/AsamK/signal-cli|A CLI and D-Bus interface for Signal|Java|
|https://github.com/signalapp/libsignal-service-java|Signal Library|Java|
|https://github.com/aaronetz/signal-bot|Bot Framework|Java|
Raw data
{
"_id": null,
"home_page": "https://github.com/filipre/signalbot",
"name": "signalbot",
"maintainer": "Ren\u00e9 Filip",
"docs_url": null,
"requires_python": ">=3.9,<4.0",
"maintainer_email": "",
"keywords": "Signal,Bot,Framework,Home Automation",
"author": "Ren\u00e9 Filip",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/e6/6f/91dfb09c91d79d292e098dcefeee7b6fecf10a894d66b464822da513be32/signalbot-0.9.2.tar.gz",
"platform": null,
"description": "# Signal Bot Framework\n\nPython package to build your own Signal bots. To run the the bot you need to start the [signal-cli-rest-api](https://github.com/bbernhard/signal-cli-rest-api) service and link your device with it. Please refer to that project for more details. The API server must run in `json-rpc` mode.\n\n## Getting Started\n\nBelow you can find a minimal example on how to use the package. There is also a bigger example in the `example` folder.\n\n```python\nimport os\nfrom signalbot import SignalBot, Command, Context\nfrom commands import PingCommand\n\n\nclass PingCommand(Command):\n async def handle(self, c: Context):\n if c.message.text == \"Ping\":\n await c.send(\"Pong\")\n\n\nif __name__ == \"__main__\":\n bot = SignalBot({\n \"signal_service\": os.environ[\"SIGNAL_SERVICE\"],\n \"phone_number\": os.environ[\"PHONE_NUMBER\"]\n })\n bot.register(PingCommand()) # all contacts and groups\n bot.start()\n```\n\nPlease 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.\n\n1. Run signal-cli-rest-api in `normal` mode first.\n```bash\ndocker run -p 8080:8080 \\\n -v $(PWD)/signal-cli-config:/home/.local/share/signal-cli \\\n -e 'MODE=normal' bbernhard/signal-cli-rest-api:0.57\n```\n\n2. Open http://127.0.0.1:8080/v1/qrcodelink?device_name=local to link your account with the signal-cli-rest-api server\n\n3. 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`.\n\n4. Restart the server in `json-rpc` mode.\n```bash\ndocker run -p 8080:8080 \\\n -v $(PWD)/signal-cli-config:/home/.local/share/signal-cli \\\n -e 'MODE=json-rpc' bbernhard/signal-cli-rest-api:0.57\n```\n\n5. 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.\n```\n...\ntime=\"2022-03-07T13:02:22Z\" level=info msg=\"Found number +491234567890 and added it to jsonrpc2.yml\"\n...\ntime=\"2022-03-07T13:02:24Z\" level=info msg=\"Started Signal Messenger REST API\"\n```\n\n6. Use the following snippet to get a group's `id`:\n```bash\ncurl -X GET 'http://127.0.0.1:8080/v1/groups/+49123456789' | python -m json.tool\n```\n\n7. Install `signalbot` and start your python script. You need to pass following environment variables to make the example run:\n- `SIGNAL_SERVICE`: Address of the signal service without protocol, e.g. `127.0.0.1:8080`\n- `PHONE_NUMBER`: Phone number of the bot, e.g. `+49123456789`\n\n```bash\nexport SIGNAL_SERVICE=\"127.0.0.1\"\nexport PHONE_NUMBER=\"+49123456789\"\npip install signalbot\npython bot.py\n```\n\n8. 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.\n```\nINFO:root:[Bot] Producer #1 started\nINFO:root:[Bot] Consumer #1 started\nINFO:root:[Bot] Consumer #2 started\nINFO:root:[Bot] Consumer #3 started\n```\n\n9. 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.\n```\nINFO:root:[Raw Message] {\"envelope\":{\"source\":\"+49123456789\",\"sourceNumber\":\"+49123456789\",\"sourceUuid\":\"fghjkl-asdf-asdf-asdf-dfghjkl\",\"sourceName\":\"Ren\u00e9\",\"sourceDevice\":3,\"timestamp\":1646000000000,\"syncMessage\":{\"sentMessage\":{\"destination\":null,\"destinationNumber\":null,\"destinationUuid\":null,\"timestamp\":1646000000000,\"message\":\"Pong\",\"expiresInSeconds\":0,\"viewOnce\":false,\"groupInfo\":{\"groupId\":\"asdasdfweasdfsdfcvbnmfghjkl=\",\"type\":\"DELIVER\"}}}},\"account\":\"+49123456789\",\"subscription\":0}\nINFO:root:[Bot] Consumer #2 got new job in 0.00046 seconds\nINFO:root:[Bot] Consumer #2 got new job in 0.00079 seconds\nINFO:root:[Bot] Consumer #2 got new job in 0.00093 seconds\nINFO:root:[Bot] Consumer #2 got new job in 0.00106 seconds\nINFO:root:[Bot] New message 1646000000000 sent:\nPong\n```\n\n## Classes and API\n\n*Documentation work in progress. Feel free to open an issue for questions.*\n\nThe package provides methods to easily listen for incoming messages and responding or reacting on them. It also provides a class to develop new commands which then can be registered within the bot.\n\n### Signalbot\n\n- `bot.register(command, contacts=True, groups=True)`: Register a new command, listen in all contacts and groups, same as `bot.register(command)`\n- `bot.register(command, contacts=False, groups=[\"Hello World\"])`: Only listen in the \"Hello World\" group\n- `bot.register(command, contacts=[\"+49123456789\"], groups=False)`: Only respond to one contact\n- `bot.start()`: Start the bot\n- `bot.send(receiver, text)`: Send a new message\n- `bot.react(message, emoji)`: React to a message\n- `bot.start_typing(receiver)`: Start typing\n- `bot.stop_typing(receiver)`: Stop typing\n- `bot.scheduler`: APScheduler > AsyncIOScheduler, see [here](https://apscheduler.readthedocs.io/en/3.x/modules/schedulers/asyncio.html?highlight=AsyncIOScheduler#apscheduler.schedulers.asyncio.AsyncIOScheduler)\n- `bot.storage`: In-memory or Redis stroage, see `storage.py`\n\n### Command\n\nTo implement your own commands, you need to inherent `Command` and overwrite following methods:\n\n- `setup(self)`: Start any task that requires to send messages already, optional\n- `describe(self)`: String to describe your command, optional\n- `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`.\n\n### Unit Testing\n\n*Note: deprecated, I want to switch to pytest eventually*\n\nIn 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:\n```python\nclass PingChatTest(ChatTestCase):\n def setUp(self):\n # initialize self.singal_bot\n super().setUp()\n # all that is left to do is to register the commands that you want to test\n self.signal_bot.register(PingCommand())\n\n @chat(\"ping\", \"ping\")\n async def test_ping(self, query, replies, reactions):\n self.assertEqual(replies.call_count, 2)\n for recipient, message in replies.results():\n self.assertEqual(recipient, ChatTestCase.group_secret)\n self.assertEqual(message, \"pong\")\n```\nIn `signalbot.utils`, check out `ReceiveMessagesMock`, `SendMessagesMock` and `ReactMessageMock` to learn more about their API.\n\n## Troubleshooting\n\n- Check that you linked your account successfully\n- Is the API server running in `json-rpc` mode?\n- Can you receive messages using `wscat` (websockets) and send messages using `curl` (http)?\n- Do you see incoming messages in the API logs?\n- Do you see the \"raw\" messages in the bot's logs?\n- Do you see \"consumers\" picking up jobs and handling incoming messages?\n- Do you see the response in the bot's logs?\n\n## Local development and package\n\n*Section work in progress. Feel free to open an issue for questions.*\n\n```bash\npoetry install\npoetry run pre-commit install\n```\n\n## Other Projects\n\nThere are a few other related projects similar to this one. You may want to check them out and see if it fits your needs.\n\n|Project|Description|Language|\n|-------|-----------|--------|\n|https://github.com/lwesterhof/semaphore|Bot Framework|Python|\n|https://git.sr.ht/~nicoco/aiosignald|signald Library / Bot Framework|Python|\n|https://gitlab.com/stavros/pysignald/|signald Library / Bot Framework|Python|\n|https://gitlab.com/signald/signald-go|signald Library|Go|\n|https://github.com/signal-bot/signal-bot|Bot Framework using Signal CLI|Python|\n|https://github.com/bbernhard/signal-cli-rest-api|REST API Wrapper for Signal CLI|Go|\n|https://github.com/bbernhard/pysignalclirestapi|Python Wrapper for REST API|Python|\n|https://github.com/AsamK/signal-cli|A CLI and D-Bus interface for Signal|Java|\n|https://github.com/signalapp/libsignal-service-java|Signal Library|Java|\n|https://github.com/aaronetz/signal-bot|Bot Framework|Java|\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Framework to create your own Signal bots",
"version": "0.9.2",
"project_urls": {
"Homepage": "https://github.com/filipre/signalbot",
"Repository": "https://github.com/filipre/signalbot"
},
"split_keywords": [
"signal",
"bot",
"framework",
"home automation"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "65f91686b2920d42d757ef5fbf691379a09b64542f3922fefb1c63551feaa0ff",
"md5": "5febb61d4e52a5ad50770cf9a12b8a7a",
"sha256": "817e6aeb3bc99525a3156a38ae33fe382d32ebb593da67aa49338e795b614c41"
},
"downloads": -1,
"filename": "signalbot-0.9.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5febb61d4e52a5ad50770cf9a12b8a7a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<4.0",
"size": 15180,
"upload_time": "2024-01-13T09:42:01",
"upload_time_iso_8601": "2024-01-13T09:42:01.895993Z",
"url": "https://files.pythonhosted.org/packages/65/f9/1686b2920d42d757ef5fbf691379a09b64542f3922fefb1c63551feaa0ff/signalbot-0.9.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e66f91dfb09c91d79d292e098dcefeee7b6fecf10a894d66b464822da513be32",
"md5": "d9b9fec6d7c2a5f89bf0c4348f3832ae",
"sha256": "61f8142ada379460f3a5a6b21aab3deea6062bddf114010e8896d2e0e004b4f0"
},
"downloads": -1,
"filename": "signalbot-0.9.2.tar.gz",
"has_sig": false,
"md5_digest": "d9b9fec6d7c2a5f89bf0c4348f3832ae",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<4.0",
"size": 15439,
"upload_time": "2024-01-13T09:42:03",
"upload_time_iso_8601": "2024-01-13T09:42:03.807479Z",
"url": "https://files.pythonhosted.org/packages/e6/6f/91dfb09c91d79d292e098dcefeee7b6fecf10a894d66b464822da513be32/signalbot-0.9.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-13 09:42:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "filipre",
"github_project": "signalbot",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "signalbot"
}