This is a Rocket.Chat incoming web hook. Hook gets an array of "messages" and silently creates chat messages directly in the Rocket's database without disturbing users with notifications or alerts - messages just appear in the channels. Messages appear silently even if the user has the channel openned: no refresh or re-enter is required (this is not this script's feature, it's how Rocket works).
This script can post messages to channels and groups by name (if message destination set to #name
), or by api roomId (no prefixes in destination). And it can post DM to a user (if destination is set to @username
). Please note, in this case DM between message author and destination user must already be created.
Note. Rocket.Chat's server version 6 has undergone significant changes. As a result, now there are two script versions: silent-post-whs-v5.js
for server version 5 and silent-post-whs-v6.js
for version 6. However, these scripts use an undocumented server API, which unfortunately could result in compatibility issues with even minor future Rocket.Chat updates.
This hook expects request.content: ISilentMessage[];
ISilentMessage {
// Message body.
text: string;
// User to set as message author.
// No leading @, user must be registered.
author: string;
// Channel to post message to.
// It may be "#channeg_or_group_name", "@username", or "room_id".
// NOTE: in case of "@username", the DM between this user and message
// author must exists, this script doesn't create one
// (greatly complicates everything).
destination: string;
// An array of message attachments. Optional, may be omitted.
attachments: [];
}
Pyhton
with requests.sessions.Session() as session:
session.post(
'https://CHAT.URL/hooks/WEBHOOK/TOKEN',
json=[
{
'text': 'Multiline\nmessage\nto #channel_by_name',
'author': 'admin',
'destination': '#channel_by_name',
},
{
'text': 'Message to abc123abc123abc123 (roomId)',
'author': 'admin',
'destination': 'abc123abc123abc123',
},
{
'text': 'DM to user `user` (by username)',
'author': 'admin',
'destination': '@user',
},
{
'text': 'Message with attachments to #channel_by_name',
'author': 'admin',
'destination': '#channel_by_name',
'attachments': [
{
"title": "Rocket.Chat",
"title_link": "https://rocket.chat",
"text": "Rocket.Chat, the best open source chat",
"image_url": "/images/integration-attachment-example.png",
"color": "#764FA5"
}
]
},
])
curl
curl -X POST -H 'Content-Type: application/json' \
--data '[ { "text": "Multiline\\nmessage\\nto #channel_by_name", "author": "admin", "destination": "#channel_by_name" }, { "text": "Message to abc123abc123abc123 (roomId)", "author": "admin", "destination": "abc123abc123abc123" }, { "text": "DM to user `user` (by username)", "author": "admin", "destination": "@user" }]' \
https://chat.url/hooks/WEBHOOK/TOKEN
This script is not working anymore as Users.findOneByUsername returns a Promise and because Messages.insertOrUpsert method does not exist now.