Skip to content

Instantly share code, notes, and snippets.

@lamoboos223
Last active March 7, 2024 12:27
Show Gist options
  • Save lamoboos223/7c608ac2ad2e30b8795fb11462f6cdf9 to your computer and use it in GitHub Desktop.
Save lamoboos223/7c608ac2ad2e30b8795fb11462f6cdf9 to your computer and use it in GitHub Desktop.
import pika
import json

# Connect to RabbitMQ
connection = pika.BlockingConnection(
    pika.ConnectionParameters('localhost', 5672)
)
channel = connection.channel()

# Declare a queue (ensure it exists)
channel.queue_declare(queue='my_queue')

# JSON data to publish
data = {'message': 'Hello, sara!', 'data': 42}

# Publish the JSON message
channel.basic_publish(
    exchange='',  # Use default exchange
    routing_key='my_queue',
    body=json.dumps(data)
)

print("Message published")

connection.close()
pip install pika jsonlib
docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management

web dashboard: http://localhost:15672/ username: guest password: guest

@lamoboos223
Copy link
Author

import pika
import json
from pika.credentials import PlainCredentials

EXCHANGE = "matrix_exchange"
QUEUE = "matrix-queue"
ROUTING_KEY = "matrix_routing_key"
USERNAME = "matrix"
PASSWORD = "matrix"
HEADERS = {
    "Transactions": [
        {
            "id": "sdljdaklja",
            "ServerName": "matrix-server",
            "ServerIp": "192.168.1.1",
            "MicroserviceFullName": "integration.notifications.nc",
            "Timestamp": "2023-08-19T19:23:43+03:00",
            "MessageType": 2,
            "PayloadType": "integration.notifications.commands.send_notification_to_users"
        }
    ]
}
PAYLOAD = {
    "tamplateId": 1921,
    "parameters": {
        "additionalProp1": {},
        "additionalProp2": {},
        "additionalProp3": {}
    },
    "email": "",
    "idNumber": 1131937658,
    "mobileNumber": ""
}

# Connect to RabbitMQ
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost', port=5672, virtual_host="/", credentials= PlainCredentials(USERNAME, PASSWORD))
)
channel = connection.channel()

# Declare a queue (ensure it exists)
channel.queue_declare(queue=QUEUE, passive=True)

props = pika.BasicProperties(headers=HEADERS)
channel.basic_publish(
    exchange=EXCHANGE,
    routing_key=ROUTING_KEY,
    body=json.dumps(PAYLOAD),
    properties=props
)

print("Message published")
connection.close()

@lamoboos223
Copy link
Author

import pika
import json
import uuid
import yaml
import datetime
from pika.credentials import PlainCredentials

with open('config.yaml', 'r') as file:
    data = yaml.safe_load(file)
[losaimi@Matrix-Syn03 rabbitmq-test]$ cat produce.py 
import pika
import json
import uuid
import yaml
import datetime
from pika.credentials import PlainCredentials

with open('config.yaml', 'r') as file:
    data = yaml.safe_load(file)

HOST=data["rabbitmq"]["host"]
PORT=data["rabbitmq"]["port"] 
VIRTUAL_HOST=data["rabbitmq"]["virtual_host"]
EXCHANGE=data["rabbitmq"]["exchange"]
QUEUE=data["rabbitmq"]["queue"] 
ROUTING_KEY=data["rabbitmq"]["routing_key"]
USERNAME=data["rabbitmq"]["username"]
PASSWORD=data["rabbitmq"]["password"] 

HEADERS = {
    "Transactions": [
        {
            "id": str(uuid.uuid4()),
            "ServerName": "TWRQA-RELEASE01",
            "ServerIp": "TWRQA-RELEASE01",
            "MicroserviceFullName": "integration.matrix",
            "Timestamp": str(datetime.datetime.now()),
            "MessageType": 2,
            "PayloadType": "integration.notifications.commands.send_notification_to_users"
        }
    ]
}

PAYLOAD = {
    "tamplateId": 1011,
    "parameters": {
        "agency_name": "MOI_MORROR"
    },
    "email": "",
    "idNumber": 1000000206,
    "mobileNumber": ""
}

# Connect to RabbitMQ

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host=HOST, port=PORT, virtual_host=VIRTUAL_HOST, credentials= PlainCredentials(USERNAME, 
PASSWORD)) )

channel = connection.channel()

# Declare a queue (ensure it exists)

channel.queue_declare(queue=QUEUE, passive=True)
props = pika.BasicProperties(headers=HEADERS)
channel.basic_publish(
    exchange=EXCHANGE,
    routing_key=ROUTING_KEY,
    body=json.dumps(PAYLOAD),
    properties=props )
print("Message published")

connection.close()
rabbitmq:
  host: "172.16.22.70"
  port: 5672
  virtual_host: "/"
  exchange : "integration.exchanges.matrix"
  queue : "integration.queues.matrix"
  routing_key : ""
  username : "integration.users.matrix"
  password : "P@ssw0rd"

@lamoboos223
Copy link
Author

import pika
import json
import uuid
import yaml
import datetime
from pika.credentials import PlainCredentials

with open('config.yaml', 'r') as file:
    data = yaml.safe_load(file)

HOST=data["rabbitmq"]["host"]
PORT=data["rabbitmq"]["port"] 
VIRTUAL_HOST=data["rabbitmq"]["virtual_host"]
EXCHANGE=data["rabbitmq"]["exchange"]
QUEUE=data["rabbitmq"]["queue"] 
ROUTING_KEY=data["rabbitmq"]["routing_key"]
USERNAME=data["rabbitmq"]["username"]
PASSWORD=data["rabbitmq"]["password"] 

HEADERS = {
    "transactions_header": {
        "Transactions": [
            {
                "Id": str(uuid.uuid4()),
                "ServerName": "TWRQA-RELEASE01",
                "ServerIp": "TWRQA-RELEASE01",
                "MicroserviceFullName": "integration.matrix",
                "Timestamp": str(datetime.datetime.now()),
                "MessageType": 2,
                "PayloadType": "integration.notifications.commands.send_notification_to_users"
            }
        ]
    }
}


PAYLOAD = {
    "tamplateId": 1011,
    "parameters": {
        "agency_name": "MOI_MORROR"
    },
    "email": "",
    "idNumber": 1000000206,
    "mobileNumber": ""
}

# Connect to RabbitMQ
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host=HOST, port=PORT, virtual_host=VIRTUAL_HOST, credentials= PlainCredentials(USERNAME, 
PASSWORD)))

channel = connection.channel()

# Declare a queue (ensure it exists)

channel.queue_declare(queue=QUEUE, passive=True)
props = pika.BasicProperties(headers=HEADERS, content_type='application/json', delivery_mode=2)
channel.basic_publish(
    exchange=EXCHANGE,
    routing_key=ROUTING_KEY,
    body=json.dumps(PAYLOAD),
    properties=props )
print("Message published")

connection.close()

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