Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Rocket.Chat notifications #1091

Open
don-rumata opened this issue Nov 12, 2022 · 6 comments
Open

Feature: Rocket.Chat notifications #1091

don-rumata opened this issue Nov 12, 2022 · 6 comments

Comments

@don-rumata
Copy link
Contributor

Hi, can you add notification support to Rocket.Chat?

Ansible doc: https://docs.ansible.com/ansible/latest/collections/community/general/rocketchat_module.html

curl example:

curl -vX POST --header "Content-Type: application/json" --data @"$JSON_FILE" https://rocketchat.example.com/hooks/$ROCKETCHAT_TOKEN

JSON_FILE:

{
  "username": "BOT_USERNAME",
  "channel": "#ROCKETCHAT_CHANNEL",
  "text": "some text"
}

ROCKETCHAT_TOKEN: qqqqqqqqqqqqqqqwwwwwwwwwwwwwweeeeeeeeeeeeee

@oussjarrousse
Copy link
Contributor

It should be very very similar to "slack" integration.
Although I am not a Go developer, I might give it a try. AI make such things possible

@oussjarrousse
Copy link
Contributor

Have you tried using a rocket.chat hook url in the slack_url in config.json?

@oussjarrousse
Copy link
Contributor

I managed to get alerts to rocket chat working, by adding a processing script to the rocketchat web hook... no changes necessary to the platform... out of the box integration is still a better idea.
so in config.json

...
"web_host": "https://your_ansible_semaphore_host",  # without a trailing /
"slack_alert": true,
"slack_url": "https://your_rocket_chat_host/hooks/hook_id/token",
...

in the dedicated rocket.chat hook add the following script, and make sure the script is enabled:

/* exported Script */
/* globals console, _, s */

/** Global Helpers
 *
 * console - A normal console instance
 * _       - An underscore instance
 * s       - An underscore string instance
 */

class Script {
  /**
   * @param {object} request
   */
  process_incoming_request({ request }) {
    // request.url.hash
    // request.url.search
    // request.url.query
    // request.url.pathname
    // request.url.path
    // request.url_raw
    // request.url_params
    // request.headers
    // request.user._id
    // request.user.name
    // request.user.username
    // request.content_raw
    // request.content

    // console is a global helper to improve debug
    console.log(request.content);
    
    // Define a mapping for color replacements
    const colorMapping = {
      'good': '#00EE00',
      'danger': '#EE0000'
    };
   const content = request.content;
   
   // Initialize content.text if it doesn't exist
   if (!content.text) {
      content.text = '';
    }

    // Check if attachments exist
    if (content.attachments && Array.isArray(content.attachments)) {
      // Iterate over attachments and modify them
      content.attachments.forEach(attachment => {
        console.log(attachment);
        // Remove "fields" and "mrkdwn_in" properties
        delete attachment.fields;
        delete attachment.mrkdwn_in;
        // Check and replace attachment.color if necessary
        if (attachment.color && colorMapping[attachment.color]) {
          attachment.color = colorMapping[attachment.color];}
        // Add attachment.text to content.text
        if (attachment.text) {
          // If it's the first addition and content.text is empty, don't prepend "; "
          if (content.text === '' || index === 0) {
            content.text += attachment.text;
          } else {
            content.text += "; " + attachment.text;
          }
        }
      });
    }
    
    console.log(content);

    // Return the modified content as response
    return {
      content: content // Return the modified content
    };
  }
}

the script will reshape the request.content to the rocket.chat format.

@oussjarrousse
Copy link
Contributor

I also created a fork of the repository and will push a pull_request soon

@oussjarrousse
Copy link
Contributor

And I wrote this blog post about it
https://blog.jarrousse.org/2024/03/16/custom-rocket-chat-webhook-to-process-3rd-party-app-messages-designated-for-slack/

fiftin added a commit that referenced this issue Mar 20, 2024
Adding alerts support for Rocket.Chat; closing Issue-#1091
@oussjarrousse
Copy link
Contributor

Feature is now available in v2.9.56. and I just successfully tested the new configurations for RocketChat:

"rocketchat_alert": true,
"rocketchat_url": "https://my_rocketchat_domain/webhook/xxxx/yyyy"

The issue can be closed :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants