Send and receive messages using the Signal Messenger. This project is a wrapper for Signal-CLI giving you a simple OOP API for using signal in a PHP environment.
Linux, PHP 7.x, JRE 11
Install the lib via composer:
composer require merlinthemagic/mtm-signal-api
If you prefer you can simply download the 3 packages separately and include their autoloaders in yur project.
require_once "/path/to/mtm-utilities/Enable.php";
require_once "/path/to/mtm-fs/Enable.php";
require_once "/path/to/mtm-signal-api/Enable.php";
You must make the signal-cli binary executable:
chmod +x /path/to/mtm-signal-api/Vendors/SignalCli/bin/signal-cli
//Secure the data directory you will be using, private keys and passphrases are stored in plaintext
//make sure the folder is writable by your php user
$safePath = "/path/to/secure/folder/";
//For testing on linux you can use the following:
//$safePath = MTM_FS_TEMP_PATH. "sigtest";
$clientObj = \MTM\SignalApi\Facts::getClients()->getSignalCli($safePath);
This will generate a URI that can be used to link the client as a slave on an existing master. This method will NOT disconnect any existing devices. It will simply allow a master device to approve the server as a linked account NOTE: This method is executed on the client object, not on any specific user. The generated user depends on the master device that accepts the link
$name = "MyTestServer"; //shows up on master as identification for this client
$uri = $clientObj->getDeviceLinkUri($name); //will throw on error
Now load the URI into your favorite QR generator or paste it to: https://www.cssscript.com/demo/pure-javascript-qr-code-generator-qrious/ (Change to allow 15% damage or you will get alot of mis reads) Then scan the QR code with a master device. Voila the server has a new linked user account that can send and receive messages
This will accept a request to link from a slave. This method will NOT disconnect any existing devices. It will simply allow a slave device to become a linked account of the master account that approves the request NOTE: This method is executed on the user object. The slave user depends on who generate the uri
$uri = "tsdevice:/?uuid=BBm...";
$userObj->linkDeviceByUri($uri); //will throw on error
WARNING! This method will disconnect any existing devices from the phone number. You will be taking over the account, not linking a new device
Goto: https://signalcaptchas.org/registration/generate.html
Complete the Captcha, hit "F12" to open developer tools. Right click the link in the console that starts with: "signalcaptcha://" Copy the link address so you can paste it to the register method below
$captcha = "signalcaptcha://03ADsBq82yYFHDEA.....";
$userObj->registerByCaptcha($captcha); //will throw on error
You will receive an SMS code on your device, use that to run the next step of verification
$userObj->registerByVoice();
$code = "234568";
$userObj->verifyBySmsCode($code); //will throw on error
$code = "863457";
$userObj->verifyByVoiceCode($code); //will throw on error
$phoneNumber = "+12134567890";
$userObj = $clientObj->getUser($phoneNumber);
$nbr = "+13106545322";
$bool = $userObj->isPhoneRegistered($nbr);
$toNbr = "+13109981100";
$msg = "Hi Mom,\nPlease buy food, im so hungry!\nThx";
$timeStamp = $userObj->sendText($toNbr, $msg);
$msg = "Hi Baby,\nPlease buy food, im so hungry!\nThx";
$timeStamp = $contactObj->sendText($msg);
$msg = "Hi Parents,\nPlease buy food, im so hungry!\nThx";
$timeStamp = $groupObj->sendText($msg);
//return array of message objects
$msgObjs = $userObj->receive(); //will throw on error
foreach ($msgObjs as $msgObj) {
if ($msgObj->getType() == "DataMessage") {
echo $msgObj->getMessage()."<br><br>";
} elseif ($msgObj->getType() == "ReceiptMessage") {
echo $msgObj->getWhen()."<br><br>";
}
}
//return array of Contact objs
$array = $userObj->getContacts(); //will throw on error
//return contact obj, null or throws
$username = "+13109997765";
$throw = false;
$contactObj = $userObj->getContactByUsername($username, $throw);
//return array of Group objs
$array = $userObj->getGroups(); //will throw on error
//return group obj, null or throws
$id = "efefekneef....";
$throw = false;
$groupObj = $userObj->getGroupById($id, $throw);
//return group obj
$name = "My Group";
$groupObj = $userObj->createGroups($name); //will throw on error
//return group obj
$name = "My New Group Name";
$groupObj->setName($name);
$adminOnly = true; //true or false
$groupObj->setEditDetailAdminOnly($adminOnly);
$adminOnly = true; //true or false
$groupObj->setAddMemberAdminOnly($adminOnly);
$enabled = true; //true or false
$withApproval = true; //true or false
$groupObj->setLinkState($enabled, $withApproval);
$contactObj = $userObj->getContactByUsername("+13109997765");
$groupObj->addMember($contactObj);
$contactObj = $userObj->getContactByUsername("+13109997765");
$groupObj->removeMember($contactObj);
//Does not return user self. Use $groupObj->getIsMember() to determine user membership
//return array of Contact objs
$array = $groupObj->getMembers();
$contactObj = $userObj->getContactByUsername("+13109997765");
$groupObj->addAdmin($contactObj);
$contactObj = $userObj->getContactByUsername("+13109997765");
$groupObj->removeAdmin($contactObj);
//Does not return user self. Use $groupObj->getIsAdmin() to determine user is admin
//return array of Contact objs
$array = $groupObj->getAdmins();
//return array of identity objects
$array = $userObj->getIdentities(); //will throw on error
//return array of device objects
$array = $userObj->getDevices(); //will throw on error