I drafted an Azure Function in Python, model V2.
I have successfully published a message into a topic.
Now I would like to set a SessionID for a published message, but can not figure out how. How do I publish a message with a SessionId?
My attempt with JSON in "my_json_string" variable was not recognized by Azure.
import logging
import azure.functions as func
from datetime import datetime
import json
app = func.FunctionApp()
# vs output into queue for python
# https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-language-python
@app.route(route="http_trigger_topic", auth_level=func.AuthLevel.ANONYMOUS)
@app.service_bus_topic_output(arg_name="message",
connection="ServiceBusConnection",
topic_name="alfdevapi6topic")
def http_trigger_topic(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
myMessage = "Hi alf this is my message via the queue to you."
logging.info(myMessage)
input_msg = req.params.get('message')
now = datetime.now()
print("now =", now)
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
my_json_string = f"""
{{
"body": "{myMessage}",
"customProperties": {{
"messagenumber": 0,
"timePublish": "{now}",
}},
"brokerProperties": {{
"SessionId": "1"
}}
}}
"""
message.set(my_json_string)
return func.HttpResponse(
"This function should process queue messages.",
status_code=200
)
2
Answers
The following combination I derived from Rithwik.
Requirements.txt
Function, which combines
@app.route(route="http_trigger")
withdef http_trigger(req: func.HttpRequest) -> func.HttpResponse:
and servicebusfrom azure.servicebus import ServiceBusClient, ServiceBusMessage
Function to send
I do agree with @Skip that
func.Out
cannot send the message with session id.But alternatively you can use below code using SDK:
Output: