import pika
import json
import requests

SLACK_WEBHOOK_URL = os.environ["SLACK_WEBHOOK_URL"]
RABBIT_MQ_SERVER = os.environ["RABBIT_MQ_SERVER"]

connection = pika.BlockingConnection(pika.ConnectionParameters(
               RABBIT_MQ_SERVER))
channel = connection.channel()
channel.queue_declare(queue='slack')

def callback(ch, method, properties, body):
    payload = {}
    payload["text"] = body
    r = requests.post(SLACK_WEBHOOK_URL, data = json.dumps(payload))

channel.basic_consume(callback, queue='slack', no_ack=True)
channel.start_consuming()

import pika
import json
import requests
import os

RABBIT_MQ_SERVER = os.environ["RABBIT_MQ_SERVER"]
RABBIT_MQ_USER = os.environ["RABBIT_MQ_USER"]
RABBIT_MQ_PWD = os.environ["RABBIT_MQ_PWD"]

credentials = pika.PlainCredentials(RABBIT_MQ_USER, RABBIT_MQ_PWD)

connection = pika.BlockingConnection(pika.ConnectionParameters(
               RABBIT_MQ_SERVER, credentials = credentials))
channel = connection.channel()
# channel.queue_declare(queue='slack')

def callback(ch, method, properties, body):
    payload = {}
    payload["text"] = body
    req = json.loads(body)
    webhook_url = json.loads(req["monitor"]["notifier"]["arguments"])["webhook_url"]
    r = requests.post(webhook_url, data = json.dumps(payload))

channel.basic_consume(callback, queue='slack', no_ack=True)
channel.start_consuming()

Add credentials + read webhook_url from notifier arguments
Add credentials + read webhook_url from notifier arguments

  import pika
  import json
  import requests
+ import os
  
- SLACK_WEBHOOK_URL = os.environ["SLACK_WEBHOOK_URL"]
  RABBIT_MQ_SERVER = os.environ["RABBIT_MQ_SERVER"]
+ RABBIT_MQ_USER = os.environ["RABBIT_MQ_USER"]
+ RABBIT_MQ_PWD = os.environ["RABBIT_MQ_PWD"]
+ 
+ credentials = pika.PlainCredentials(RABBIT_MQ_USER, RABBIT_MQ_PWD)
  
  connection = pika.BlockingConnection(pika.ConnectionParameters(
-                RABBIT_MQ_SERVER))
+                RABBIT_MQ_SERVER, credentials = credentials))
  channel = connection.channel()
- channel.queue_declare(queue='slack')
+ # channel.queue_declare(queue='slack')
  
  def callback(ch, method, properties, body):
      payload = {}
      payload["text"] = body
+     req = json.loads(body)
+     webhook_url = json.loads(req["monitor"]["notifier"]["arguments"])["webhook_url"]
-     r = requests.post(SLACK_WEBHOOK_URL, data = json.dumps(payload))
+     r = requests.post(webhook_url, data = json.dumps(payload))
  
  channel.basic_consume(callback, queue='slack', no_ack=True)
  channel.start_consuming()
  
Add credentials + read webhook_url from notifier arguments
## Code Before:
import pika
import json
import requests

SLACK_WEBHOOK_URL = os.environ["SLACK_WEBHOOK_URL"]
RABBIT_MQ_SERVER = os.environ["RABBIT_MQ_SERVER"]

connection = pika.BlockingConnection(pika.ConnectionParameters(
               RABBIT_MQ_SERVER))
channel = connection.channel()
channel.queue_declare(queue='slack')

def callback(ch, method, properties, body):
    payload = {}
    payload["text"] = body
    r = requests.post(SLACK_WEBHOOK_URL, data = json.dumps(payload))

channel.basic_consume(callback, queue='slack', no_ack=True)
channel.start_consuming()

## Instruction:
Add credentials + read webhook_url from notifier arguments
## Code After:
import pika
import json
import requests
import os

RABBIT_MQ_SERVER = os.environ["RABBIT_MQ_SERVER"]
RABBIT_MQ_USER = os.environ["RABBIT_MQ_USER"]
RABBIT_MQ_PWD = os.environ["RABBIT_MQ_PWD"]

credentials = pika.PlainCredentials(RABBIT_MQ_USER, RABBIT_MQ_PWD)

connection = pika.BlockingConnection(pika.ConnectionParameters(
               RABBIT_MQ_SERVER, credentials = credentials))
channel = connection.channel()
# channel.queue_declare(queue='slack')

def callback(ch, method, properties, body):
    payload = {}
    payload["text"] = body
    req = json.loads(body)
    webhook_url = json.loads(req["monitor"]["notifier"]["arguments"])["webhook_url"]
    r = requests.post(webhook_url, data = json.dumps(payload))

channel.basic_consume(callback, queue='slack', no_ack=True)
channel.start_consuming()
