Message Actions API for Python SDK
Message actions let a PubNub client for Python add or remove metadata on published messages, such as receipts and reactions. A client that subscribes to a channel receives message action events through an event listener. A client also fetches past message actions from Message Persistence, either separately or together with the original messages.
For the concepts behind message actions, see Message actions.
Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
-
.sync()returns anEnvelopeobject, which has two fields:Envelope.result, whose type differs for each API, andEnvelope.statusof typePnStatus.1pubnub.publish() \
2 .channel("myChannel") \
3 .message("Hello from PubNub Python SDK") \
4 .sync() -
.pn_async(callback)returnsNoneand passes the values ofEnvelope.resultandEnvelope.statusto a callback you must define beforehand.1def my_callback_function(result, status):
2 print(f'TT: {result.timetoken}, status: {status.category.name}')
3
4pubnub.publish() \
5 .channel("myChannel") \
6 .message("Hello from PubNub Python SDK") \
7 .pn_async(my_callback_function)
Add message action
Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal.
Requires Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
add_message_action() adds an action to a published message through a PubNub client for Python and returns the added action.
channelrequired#- The ID of the channel that stores the target message. An empty value is invalid. An empty value fails validation with the message
Channel missing. message_actionrequired#- The message action to add, a
PNMessageActionobject with thetype,valueandmessage_timetokenattributes set. A missing object fails validation with the messageMessage action is missing. - property of message_action:
typerequired# - The type of the message action, a string that names the kind of metadata, such as
reactionorreceipt. An empty value is invalid. An empty value fails validation with the messageMessage action type is missing. - property of message_action:
valuerequired# - The value of the message action, a string that carries the metadata, such as
smiley_faceorread. An empty value is invalid. An empty value fails validation with the messageMessage action value is missing. - property of message_action:
message_timetokenrequired# - The timetoken of the published message to add the action to. A missing value fails validation with the message
Message timetoken is missing.
Sample code
Returns
The method returns an Envelope. Its result property is a PNAddMessageActionResult that holds the added message action, and its status property is a PNStatus.
result#- The added message action.
- property of result:
type# - The
typethat the request set. - property of result:
value# - The
valuethat the request set. - property of result:
uuid# - The User ID of the client that added the action.
- property of result:
action_timetoken# - The timetoken at which PubNub stored the action. Pass it to
remove_message_action()to remove the action. - property of result:
message_timetoken# - The timetoken of the message that has the action.
status#- The operation status.
PNMessageAction
A PNMessageAction object describes one message action. You build one to add an action, and the SDK returns a list of them when you get message actions.
| Attribute | Type | Description |
|---|---|---|
type | str | The kind of metadata, such as reaction or receipt. |
value | str | The metadata itself, such as smiley_face or read. |
message_timetoken | int or str | The timetoken of the message that has the action. |
uuid | str | The User ID of the client that added the action. Set on objects that the SDK returns. |
action_timetoken | str | The timetoken at which PubNub stored the action. Set on objects that the SDK returns. |
Create an object from a dictionary that holds the camelCase keys of the REST API response.
Examples for Add message action
Syntax
1pubnub.add_message_action() \
2 .channel(channel: str) \
3 .message_action(message_action: PNMessageAction) \
4 .sync() -> PNAddMessageActionResultEnvelope
Basic usage
1from pubnub.pnconfiguration import PNConfiguration
2from pubnub.pubnub import PubNub
3
4pnconfig = PNConfiguration()
5pnconfig.subscribe_key = "my_subkey"
6pnconfig.publish_key = "my_pubkey"
7pnconfig.ssl = True
8pnconfig.user_id = "my_custom_user_id"
9pubnub = PubNub(pnconfig)
Basic usage (Builder Pattern)
1from pubnub.models.consumer.message_actions import PNMessageAction
2msg_action = PNMessageAction()
3msg_action.type = "reaction"
4msg_action.value = "smiley_face"
5msg_action.message_timetoken = message_timetoken
6
7envelope = pubnub.add_message_action() \
8 .channel("chats.room1") \
9 .message_action(msg_action) \
10 .sync()
11
12print(f"Response: {envelope.result.__dict__}")
Basic usage (Named Arguments)
1from pubnub.models.consumer.message_actions import PNMessageAction
2
3msg_action = PNMessageAction()
4msg_action.type = "reaction"
5msg_action.value = "thumbs_up"
6msg_action.message_timetoken = message_timetoken
7
8envelope = pubnub.add_message_action(
9 channel="chats.room1",
10 message_action=msg_action
11).sync()
12
13print(f"Response: {envelope.result.__dict__}")
Returns
1{
2 "action_timetoken": "15956343330507960",
3 "message_timetoken": "1595634332",
4 "type": "reaction",
5 "uuid": "my_uuid",
6 "value": "smiley_face"
7}
PNMessageAction
1action = PNMessageAction({
2 'uuid': 'user1',
3 'type': 'reaction',
4 'value': 'smiley_face',
5 'actionTimetoken': '15901706735798836',
6 'messageTimetoken': '15901706735795200',
7})
Remove message action
Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal.
Requires Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
remove_message_action() removes an action from a published message through a PubNub client for Python.
channelrequired#- The ID of the channel that stores the message with the action. An empty value is invalid. An empty value fails validation with the message
Channel missing. message_timetokenrequired#- The timetoken of the published message that has the action. A missing value fails validation with the message
Message timetoken is missing. action_timetokenrequired#- The timetoken of the message action to remove. The add operation returns this timetoken when it stores the action. A missing value fails validation with the message
Message action timetoken is missing.
Sample code
Returns
The method returns an Envelope.
Examples for Remove message action
Syntax
1pubnub.remove_message_action() \
2 .channel(channel: str) \
3 .message_timetoken(message_timetoken: int) \
4 .action_timetoken(action_timetoken: int) \
5 .sync() -> Envelope
Basic usage (Builder Pattern)
1envelope = pubnub.remove_message_action() \
2 .channel("chats.room1") \
3 .action_timetoken(action_timetoken) \
4 .message_timetoken(message_timetoken) \
5 .sync()
Basic usage (Named Arguments)
1envelope = pubnub.remove_message_action(channel="chats.room1",
2 action_timetoken=action_timetoken,
3 message_timetoken=message_timetoken) \
4 .sync()
Returns
1{}
Get message actions
Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal.
Requires Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
get_message_actions() gets the stored message actions of a channel through a PubNub client for Python, within an optional range of action timetokens.
The response lists message actions in ascending order of their action timetokens, oldest first. PubNub can return fewer message actions than the requested range holds when the request reaches an internal limit. To fetch the rest, send another request with adjusted start and end values. The Python result exposes only the actions list and no more value.
channelrequired#- The ID of the channel to get message actions for. An empty value is invalid. An empty value fails validation with the message
Channel missing. start#- The newer boundary of the range, as an action timetoken. The range is exclusive, so the response includes only actions with a lower timetoken. When you set both
startandend,startmust be the higher value. end#- The older boundary of the range, as an action timetoken. The range is inclusive, so the response includes actions with a timetoken equal to or higher than
end. The SDK sendsendtogether withstart. limit#- The number of message actions to return in one response. The SDK takes the limit as a string, for example
"50", and sends it only when it differs from the default.
Sample code
Returns
The method returns an Envelope. Its result property is a PNGetMessageActionsResult.
Examples for Get message actions
Syntax
1pubnub.get_message_actions() \
2 .channel(channel: str) \
3 .start(start: str) \
4 .end(end: str) \
5 .limit(limit: str) \
6 .sync() -> PNGetMessageActionsResultEnvelope
Basic usage (Builder Pattern)
1envelope = pubnub.get_message_actions() \
2 .channel("chats.room1") \
3 .start(start) \
4 .end(end) \
5 .limit("50") \
6 .sync()
7
8for action in envelope.result.actions:
9 print(f"Message Action type: {action.type}")
10 print(f"Message Action value: {action.value}")
11 print(f"Message Action timetoken: {action.message_timetoken}")
12 print(f"Message Action uuid: {action.uuid}")
13 print(f"Message Action timetoken: {action.action_timetoken}")
Basic usage (Named Arguments)
1envelope = pubnub.get_message_actions(channel="chats.room1", start=start, end=end, limit="50").sync()
Returns
1{
2 "actions": [
3 {
4 "action_timetoken": "15956373593404068",
5 "message_timetoken": "15956342921084730",
6 "type": "reaction",
7 "uuid": "my_uuid",
8 "value": "smiley_face"
9 }
10 ]
11}