Message Actions API for Python-Asyncio 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.

Message Actions vs. Message Reactions

Message Actions is the flexible, low-level API for adding any metadata to messages (read receipts, delivery confirmations, custom data), while Message Reactions specifically refers to using Message Actions for emoji/social reactions.

In PubNub Core and Chat SDKs, the same underlying Message Actions API is referred to as Message Reactions when used for emoji reactions - it's the same functionality, just different terminology depending on the use case.

Add Message Action

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Add an action on a published message. Returns the added action in the response.

Method(s)

To Add a Message Action you can use the following method(s) in the Python-asyncio SDK:

pubnub.add_message_action() \
.channel(String) \
.message_action(PNMessageAction) \
.pn_async(Function message_action_callback)
* required
ParameterDescription
channel *
Type: String
The channel name to which to add the message action
message_action *
Type: PNMessageAction
Message action information
message_action.type *
Type: String
What feature this message action represents
message_action.value *
Type: String
Value to be stored along with the message action
message_action.message_timetoken *
Type: Integer
Timetoken of the message to which to add the action
message_action_callback *
Type: Function
Handles returned data for successful and unsuccessful add message action operations. Details on the callback are here

Basic Usage

Reference code

This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

import asyncio
import os
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio
from pubnub.exceptions import PubNubException
from pubnub.models.consumer.pubsub import PNMessageAction


async def add_message_action(pubnub: PubNubAsyncio):
try:
# Create a message action
msg_action = PNMessageAction()
msg_action.type = "reaction"
msg_action.value = "smiley_face"
show all 49 lines

Returns

# Example of status
{
'affected_channels': None,
'affected_channels_groups': None,
'affected_groups': None,
'auth_key': None,
'category': 2,
'client_response': None,
'error': None,
'error_data': None,
'operation': 42,
'origin': 'ps.pndsn.com',
'status_code': 200,
'tls_enabled': True,
'uuid': 'my_uuid'
show all 25 lines

Remove Message Action

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Remove a previously added action on a published message. Returns an empty response.

Method(s)

To Remove a Message Action 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)
* required
ParameterDescription
channel *
Type: String
The channel name from which to remove the message action
action_timetoken *
Type: Integer
Timetoken of the message action to be removed
message_timetoken *
Type: Integer
Timetoken of the message from which to remove the action
message_action_callback *
Type: Function
Handles returned data for successful and unsuccessful remove message action operations. Details on the callback are here

Basic Usage

pubnub.remove_message_action()\
.channel("chats.room1")\
.action_timetoken(15956346328442840)\
.message_timetoken(1595634632)\
.pn_async(message_action_callback)

Returns

# Example of status
{
'affected_channels': None,
'affected_channels_groups': None,
'affected_groups': None,
'auth_key': None,
'category': 2,
'client_response': None,
'error': None,
'error_data': None,
'operation': 44,
'origin': 'ps.pndsn.com',
'status_code': 200,
'tls_enabled': True,
'uuid': 'my_uuid'
show all 19 lines

Get Message Actions

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

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 Actions you can use the following method(s) in the Python-asyncio SDK:

pubnub.get_message_actions() \
.channel(String) \
.start(String) \
.end(String) \
.limit(Integer) \
.pn_async(message_action_callback)
* required
ParameterDescription
channel *
Type: String
The channel name for which to retrieve the list of message actions
start
Type: String
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
Type: String
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
Type: Integer
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 *
Type: Function
Handles returned data for successful and unsuccessful retrieve message action operations. Details on the callback are here

Basic Usage

# Retrieve all actions on a single message

pubnub.get_message_actions()\
.channel("chats.room1")\
.start("15956342921084731")\
.end("15956342921084730")\
.limit(50)\
.pn_async(message_action_callback)

Returns

# Example of status
{
'affected_channels': None,
'affected_channels_groups': None,
'affected_groups': None,
'auth_key': None,
'category': 2,
'client_response': None,
'error': None,
'error_data': None,
'operation': 43,
'origin': 'ps.pndsn.com',
'status_code': 200,
'tls_enabled': True,
'uuid': 'my_uuid'
show all 29 lines

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("Uh oh. We had a problem sending the message. :( \n %s" % status)
else:
if isinstance(envelope, PNAddMessageActionResult):
print("Message Action type: %s" % envelope.type)
print("Message Action value: %s" % envelope.value)
print("Message Action timetoken: %s" % envelope.message_timetoken)
print("Message Action uuid: %s" % envelope.uuid)
print("Message Action timetoken: %s" % 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
Last updated on