/** * Send a message to a Threema ID. * * @param Threema\MsgApi\Connection $connector connector * @param string $threemaId The id where the message should be send to * @param string $message The message to send (max 3500 characters) * * @throws Exception * @return int */ function SendMessage(Threema\MsgApi\Connection $connector, $threemaId, $message) { $e2eHelper = new \Threema\MsgApi\Helpers\E2EHelper(KeyHexToBin(MSGAPI_PRIVATE_KEY), $connector); $result = $e2eHelper->sendTextMessage($threemaId, $message); if (true === $result->isSuccess()) { return $result->getMessageId(); } else { throw new Exception($result->getErrorMessage()); } }
<?php use Threema\MsgApi\Connection; use Threema\MsgApi\ConnectionSettings; //include_project require_once 'bootstrap.php'; //define your connection settings $settings = new ConnectionSettings('*YOUR_GATEWAY_THREEMA_ID', 'YOUR_GATEWAY_THREEMA_ID_SECRET'); //public key store file //best practice: create a publickeystore //$publicKeyStore = new Threema\MsgApi\PublicKeyStores\PhpFile('keystore.php'); $publicKeyStore = null; //create a connection $connector = new Connection($settings, $publicKeyStore); $senderPrivateKey = "MY_PUBLIC_KEY_IN_BIN"; $e2eHelper = new \Threema\MsgApi\Helpers\E2EHelper($senderPrivateKey, $connector); $result = $e2eHelper->sendTextMessage("TEST1234", "This is an end-to-end encrypted message"); if (true === $result->isSuccess()) { echo 'Message ID: ' . $result->getMessageId() . "\n"; } else { echo 'Error: ' . $result->getErrorMessage() . "\n"; }