Message Reactions API for PubNub Python SDK
Add or remove actions on published messages to build features like receipts, reactions, or to associate custom metadata to messages. Clients can subscribe to a channel to receive message action events on that channel. They can also fetch past message actions from Message Persistence independently or when they fetch original messages.
Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
-
.sync()
returns anEnvelope
object, which has two fields:Envelope.result
, whose type differs for each API, andEnvelope.status
of typePnStatus
.pubnub.publish() \
.channel("myChannel") \
.message("Hello from PubNub Python SDK") \
.sync() -
.pn_async(callback)
returnsNone
and passes the values ofEnvelope.result
andEnvelope.status
to a callback you must define beforehand.def my_callback_function(result, status):
print(f'TT: {result.timetoken}, status: {status.category.name}')
pubnub.publish() \
.channel("myChannel") \
.message("Hello from PubNub Python SDK") \
.pn_async(my_callback_function)
Add Message Reaction
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Add an action on a published message
. Returns the added action in the response.
Method(s)
To Add a Message Reaction you can use the following method(s) in the Python SDK:
pubnub.add_message_action() \
.channel(String) \
.message_action(PNMessageAction) \
.pn_async(Function message_action_callback)
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | The channel name to which to add the message action |
message_action | PNMessageAction | Yes | Message action information |
message_action.type | String | Yes | What feature this message action represents |
message_action.value | String | Yes | Value to be stored along with the message action |
message_action.message_timetoken | Integer | Yes | Timetoken of the message to which to add the action |
message_action_callback | Function | Yes | Handles returned data for successful and unsuccessful add message action operations. Details on the callback are here |
Basic Usage
- Builder Pattern
- Named Arguments
from pubnub.models.consumer.message_actions import PNMessageAction
msg_action = PNMessageAction()
msg_action.type = "reaction"
msg_action.value = "smiley_face"
msg_action.message_timetoken = str(int(time.time()))
pubnub.add_message_action()\
.channel("chats.room1")\
.message_action(msg_action)\
.pn_async(message_action_callback)
from pubnub.models.consumer.message_actions import PNMessageAction
msg_action = PNMessageAction()
msg_action.type = "reaction"
msg_action.value = "smiley_face"
msg_action.message_timetoken = str(int(time.time()))
pubnub.add_message_action(channel="chats.room1", message_action=msg_action).pn_async(message_action_callback)
Returns
The add_message_action()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNAddMessageActionResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNAddMessageActionResult
{
'action_timetoken': '15956343330507960',
'message_timetoken': '1595634332',
'type': 'reaction',
'uuid': 'my_uuid',
'value': 'smiley_face'
}
Remove Message Reaction
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Remove a previously added action on a published message
. Returns an empty response.
Method(s)
To Remove a Message Reaction you can use the following method(s) in the JavaScript SDK:
pubnub.remove_message_action() \
.channel(String) \
.action_timetoken(Integer) \
.message_timetoken(Integer) \
.pn_async(message_action_callback)
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | The channel name from which to remove the message action |
action_timetoken | Integer | Yes | Timetoken of the message action to be removed |
message_timetoken | Integer | Yes | Timetoken of the message from which to remove the action |
message_action_callback | Function | Yes | Handles returned data for successful and unsuccessful remove message action operations. Details on the callback are here |
Basic Usage
- Builder Pattern
- Named Arguments
pubnub.remove_message_action()\
.channel("chats.room1")\
.action_timetoken(15956346328442840)\
.message_timetoken(1595634632)\
.pn_async(message_action_callback)
from pubnub.models.consumer.message_actions import PNMessageAction
msg_action = PNMessageAction()
msg_action.type = "reaction"
msg_action.value = "smiley_face"
msg_action.message_timetoken = str(int(time.time()))
pubnub.remove_message_action(channel="chats.room1",
action_timetoken=15956346328442840,
message_timetoken=1595634632) \
.pn_async(message_action_callback)
Returns
The remove_message_action()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNRemoveMessageActionResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNRemoveMessageActionResult
# in case of success (empty object)
{}
Get Message Reactions
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Get a list of message actions in a channel
. Returns a list of actions sorted by the action's timetoken in ascending order.
Method(s)
To Get Message Reactions you can use the following method(s) in the Python SDK:
pubnub.get_message_actions() \
.channel(String) \
.start(String) \
.end(String) \
.limit(Integer) \
.pn_async(message_action_callback)
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | The channel name for which to retrieve the list of message actions |
start | String | No | Message action timetoken denoting the start of the range requested. Return values will be less than start . If not specified, defaults to the current time. |
end | String | No | Message action timetoken denoting the end of the range requested. Return values will be greater than or equal to end . If start is specified, end must be less than or equal to start . |
limit | Integer | No | Maximum number of message actions to return in the response. If the number of results exceeds this limit, the results will include a more token. Refer to the REST API documentation for details. |
message_action_callback | Function | Yes | Handles returned data for successful and unsuccessful retrieve message action operations. Details on the callback are here |
Basic Usage
- Builder Pattern
- Named Arguments
# Retrieve all reactions on a single message
pubnub.get_message_actions() \
.channel("chats.room1") \
.start("15956342921084731") \
.end("15956342921084730") \
.limit(50) \
.pn_async(message_action_callback)
pubnub.get_message_actions(channel="chats.room1",
start="15956342921084731",
end="15956342921084730",
limit=50) \
.pn_async(message_action_callback)
Returns
The get_message_actions
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNGetMessageActionsResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNGetMessageActionsResult
{
'actions': [
{
'actionTimetoken': '15956373593404068',
'messageTimetoken': '15956342921084730',
'type': 'reaction',
'uuid': 'my_uuid',
'value': 'smiley_face'
}
]
}
PNMessageAction Callback
The structure of a PNMessageAction
is as follows:
action = PNMessageAction({
'uuid': 'user1',
'type': 'reaction',
'value': 'smiley_face',
'actionTimetoken': '15901706735798836',
'messageTimetoken': '15901706735795200',
})
The following is a sample message_action_callback
method you can use as a starting point for your own implementation:
def message_action_callback(envelope, status):
if status.is_error():
print(f"Uh oh. We had a problem sending the message. :( \n {status}")
return
if isinstance(envelope, PNAddMessageActionResult):
print(f"Message Action type: {envelope.type}")
print(f"Message Action value: {envelope.value}")
print(f"Message Action timetoken: {envelope.message_timetoken}")
print(f"Message Action uuid: {envelope.uuid}")
print(f"Message Action timetoken: {envelope.action_timetoken}")
elif isinstance(envelope, PNRemoveMessageActionResult):
# Envelope here is an empty dictionary {}
pass
elif isinstance(envelope, PNGetMessageActionsResult):
print("Message Actions Result:\n")
show all 22 lines