Unable to trigger ServiceBusTrigger function app written in python in azure

49 Views Asked by At

I am using two function apps - HttpTrigger and ServiceBusTrigger. In Httptrigger I am writing data to service bus queue, which is working and I am able to see message in Service Bus explorer. I want to trigger another function based on Service Bus which will send emails with the message present in queue.

This is my HttpTrigger Method:

import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage
import logging
import azure.functions as func
CONNECTION_STR = Provided
QUEUE_NAME = Provided
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        message = ServiceBusMessage("Single Message")
        servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)
        with servicebus_client:
            servicebus_client.get_queue_sender(queue_name=QUEUE_NAME).send_messages(message)
        logging.info("Done Sending messages")
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

``

I am using azure portal to Test the Function, in this: enter image description here

This is function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

This is my ServiceBusTrigger Function:

Code:

import logging
import os
import smtplib
from email.message import EmailMessage
import azure.functions as func
import json
fromaddr = os.environ['EMAIL']
pass_word = os.environ['PASSWORD']
def main(msg: func.ServiceBusMessage):
    rcv_msg = msg.get_body().decode('utf-8')
    logging.info('Python ServiceBus queue trigger processed message: %s', rcv_msg)
    send_email_function(rcv_msg)

This is function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "chronos-service-bus-queue",
      "connection": "chronosservicebus_SERVICEBUS"
    }
  ]
}```

This is the Integration trigger:
[![Integration in servicebustrigger][1]][1]

In code instead of msg, I tried name as well. But still nothing.


  [1]: https://i.stack.imgur.com/55G62.png
1

There are 1 best solutions below

1
Vaibhav Patil On
Use below Trigger function 
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage
import logging
import azure.functions as func

CONNECTION_STR = "your_service_bus_connection_string"  # Replace with your actual connection string
QUEUE_NAME = "chronos-service-bus-queue"  # Replace with your actual queue name

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        message = ServiceBusMessage("Single Message")
        servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)
        with servicebus_client:
            sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME)
            sender.send_messages(message)
        logging.info("Done Sending messages to Service Bus")
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )