I need to send direct message to target user to notify user action in my PHP application. My company already has
Rocket.Chat messaging system as a
Company Messenger. So I decide to use it. Here is a sample.
Direct Message from
Bot to user,
Bot role has to have
create-d permission. Go to "Administration" -> "Permissions" and check
create-d permission for
This step is
optional if you want to use existing bot user as a sender of this notification.
Go to "Administration" -> "Users" and create a new
Bot user and activate.
Go to "Administration" -> "Integrations" and create a new "Incoming WebHook". And input each settings like this:
Save Changes to save.
You can see the
curl example on page. Copy it and execute from command prompt to test. If you can receive notification message, all settings are correct.
But if you didn't receive, please check this;
create-dpermission. You already give permission or not?
You can see
WebHook URL on
Incoming WebHook page. Copy it to use in your php application.
We need a mapping betwwen
PHP application user and
Rocket.Chat user. You have to make like
Rocket.Chat Integration page to let user input Rocket.Chat username, or use
same username between them.
We will use
guzzle as a HTTP Client. Install
$ composer require guzzlehttp/guzzle
Write coding like bellow to send direct message to user.
$client = new GuzzleHttpClient();
$client->post('Paste Your WebHook URL Here', [
'body' => json_encode([
'channel' => '@' . $username,
'username' => 'My PHP Project',
'text' => 'New notification arrived',
'attachments' => [[
'title' => 'Notification Title',
'title_link' => 'http://www.example.com/123',
'text' => 'Notification description',
'color' => '#0000FF'
See Guzzle Documentation for more options.