Gediminas Lelešius

Signal bot in Python

2021-10-14, U: 2021-11-29

Install Signal-cli

Download latest version directly from releases: https://github.com/AsamK/signal-cli/releases.
Extract it and try to run signal-cli -u [PHONE NUMBER] receive.
If libsignal fails to load because architectures don't match, go to https://github.com/exquo/signal-libs-build/releases.

Short version of Signal-cli guide https://github.com/AsamK/signal-cli/wiki/Provide-native-lib-for-libsignal:
Choose your architecture and download latest version (dpkg --print-architecture).
Go to signal-cli/lib, remove included versions of libraries and add downloaded ones:

zip -d signal-client-java-*.jar libsignal_jni.so
zip -d zkgroup-java-*.jar libzkgroup.so

zip -u signal-client-java-*.jar libsignal_jni.so
zip -u zkgroup-java-*.jar libzkgroup.so

Log in to Signal

As main device (not tested!)
signal-cli --config /var/lib/signal-cli -u [PHONE NUMBER] register
signal-cli --config /var/lib/signal-cli -u [PHONE NUMBER] verify [CODE FROM SMS]
As secondary device
signal-cli --config /var/lib/signal-cli link -n "[DEVICE NAME]"

Create DBus service

https://github.com/AsamK/signal-cli/wiki/DBus-service

Write bot

Install python dependencies:

pip install git+https://github.com/LEW21/pydbus.git
pip install PyGObject
pip install pycairo

Simple echo bot:

from pydbus import SystemBus
from gi.repository import GLib
import base64

bus = SystemBus()
loop = GLib.MainLoop()

signal = bus.get('org.asamk.Signal')

def send_message(source, groupID, text, attachments=[]):
    if len(groupID) == 0:
        signal.sendMessage(text, attachments, str(source))
    else:
        signal.sendGroupMessage(text, attachments, groupID)


def msgRcv (timestamp, source, groupID, message, attachments):
    send_message(source, groupID, 'text', ['attachment_file_path'])


signal.onMessageReceived = msgRcv
print("START")
loop.run()