Skip to content

Commit 6ffa036

Browse files
committed
Add Service Bus Topic code for Python
1 parent 4695ee5 commit 6ffa036

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

articles/azure-functions/functions-bindings-service-bus-trigger.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def test_function(msg: func.ServiceBusMessage):
227227

228228
# [v1](#tab/python-v1)
229229

230-
A Service Bus binding is defined in *function.json* where *type* is set to `serviceBusTrigger`.
230+
A Service Bus binding is defined in *function.json* where *type* is set to `serviceBusTrigger` and the queue is set by `queueName`.
231231

232232
```json
233233
{
@@ -277,6 +277,79 @@ def main(msg: func.ServiceBusMessage):
277277

278278
---
279279

280+
The following example demonstrates how to read a Service Bus queue topic via a trigger.
281+
282+
# [v2](#tab/python-v2)
283+
284+
```python
285+
import logging
286+
import azure.functions as func
287+
288+
app = func.FunctionApp()
289+
290+
@app.function_name(name="ServiceBusTopicTrigger1")
291+
@app.service_bus_topic_trigger(arg_name="message",
292+
topic_name="TOPIC_NAME",
293+
connection="CONNECTION_SETTING",
294+
subscription_name="SUBSCRIPTION_NAME")
295+
def test_function(message: func.ServiceBusMessage):
296+
message_body = message.get_body().decode("utf-8")
297+
logging.info("Python ServiceBus topic trigger processed message.")
298+
logging.info("Message Body: " + message_body)
299+
```
300+
301+
# [v1](#tab/python-v1)
302+
303+
A Service Bus binding is defined in *function.json* where *type* is set to `serviceBusTrigger` and the topic is set by `topicName`.
304+
305+
```json
306+
{
307+
"scriptFile": "__init__.py",
308+
"bindings": [
309+
   {
310+
     "type": "serviceBusTrigger",
311+
     "direction": "in",
312+
     "name": "msg",
313+
     "topicName": "inputtopic",
314+
     "connection": "AzureServiceBusConnectionString"
315+
   }
316+
]
317+
}
318+
```
319+
320+
The code in *_\_init_\_.py* declares a parameter as `func.ServiceBusMessage`, which allows you to read the topic in your function.
321+
322+
```python
323+
import json
324+
325+
import azure.functions as azf
326+
327+
328+
def main(msg: azf.ServiceBusMessage) -> str:
329+
result = json.dumps({
330+
'message_id': msg.message_id,
331+
'body': msg.get_body().decode('utf-8'),
332+
'content_type': msg.content_type,
333+
'delivery_count': msg.delivery_count,
334+
'expiration_time': (msg.expiration_time.isoformat() if
335+
msg.expiration_time else None),
336+
'label': msg.label,
337+
'partition_key': msg.partition_key,
338+
'reply_to': msg.reply_to,
339+
'reply_to_session_id': msg.reply_to_session_id,
340+
'scheduled_enqueue_time': (msg.scheduled_enqueue_time.isoformat() if
341+
msg.scheduled_enqueue_time else None),
342+
'session_id': msg.session_id,
343+
'time_to_live': msg.time_to_live,
344+
'to': msg.to,
345+
'user_properties': msg.user_properties,
346+
})
347+
348+
logging.info(result)
349+
```
350+
351+
---
352+
280353
::: zone-end
281354
::: zone pivot="programming-language-csharp"
282355
## Attributes

0 commit comments

Comments
 (0)