Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Send message): 🐛 Fixed bug sending an empty context #59

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 44 additions & 49 deletions gupshup_matrix/gupshup/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,37 @@ def __init__(self, config: Config, loop: asyncio.AbstractEventLoop) -> None:
self.sender = config["gupshup.sender"]
self.http = ClientSession(loop=loop)

def process_message_context(
self, message: Dict, additional_data: Optional[Dict] = None
) -> str:
"""
Format the message to be sent to Gupshup.

Parameters
----------
message: dict
The dict with the message that will be sent to Gupshup.
additional_data: dict
Contains the id of the message that the user is replying to.

Returns
----------
str
The formatted message.
"""
if additional_data and additional_data.get("context"):
message["context"] = additional_data.get("context")

return json.dumps(message)

async def send_message(
self,
data: dict,
data: Dict,
body: Optional[str] = None,
msgtype: Optional[str] = None,
media: Optional[str] = None,
is_gupshup_template: bool = False,
additional_data: Optional[dict] = {},
additional_data: Optional[Dict] = {},
) -> Dict[str, str]:
"""
Send a message to a user.
Expand Down Expand Up @@ -57,57 +80,29 @@ async def send_message(
"""
headers = data.pop("headers")

if body and msgtype is None and not is_gupshup_template:
data["message"] = json.dumps(
{
"isHSM": "false",
"type": "text",
"text": body,
"context": additional_data.get("context", {}),
}
)
# If the message is a interactive message, the additional_data is a dict with the quick
# replies or lists, otherwise additional_data has an id of a message that
# the user is replying to
elif msgtype == "m.interactive_message":
if msgtype == "m.interactive_message":
jcardenas3 marked this conversation as resolved.
Show resolved Hide resolved
data["message"] = json.dumps(additional_data)
else:
data["message"] = json.dumps(
{
"isHSM": "true",
"type": "text",
"text": body,
"context": additional_data.get("context", {}),
}
)

if media:
if msgtype == MessageType.IMAGE:
data["message"] = json.dumps(
{
"type": "image",
"originalUrl": media,
"previewUrl": media,
"context": additional_data.get("context", {}),
}
)
elif msgtype == MessageType.VIDEO:
data["message"] = json.dumps(
{"type": "video", "url": media, "context": additional_data.get("context", {})}
)
elif msgtype == MessageType.AUDIO:
data["message"] = json.dumps(
{"type": "audio", "url": media, "context": additional_data.get("context", {})}
)
elif msgtype == MessageType.FILE:
data["message"] = json.dumps(
{
"type": "file",
"url": media,
"filename": body,
"context": additional_data.get("context", {}),
}
)
if body and msgtype == MessageType.TEXT:
message_dict = {"type": "text", "text": body}

if media:
if msgtype == MessageType.IMAGE:
message_dict = {"type": "image", "originalUrl": media, "previewUrl": media}

elif msgtype == MessageType.VIDEO:
message_dict = {"type": "video", "url": media}

elif msgtype == MessageType.AUDIO:
message_dict = {"type": "audio", "url": media}

elif msgtype == MessageType.FILE:
message_dict = {"type": "file", "url": media, "filename": body}

data["message"] = self.process_message_context(message_dict, additional_data)

self.log.debug(f"Sending message {data}")

Expand Down Expand Up @@ -157,7 +152,7 @@ async def mark_read(self, message_id: GupshupMessageID, gupshup_app: DBGupshupAp
self.log.debug(f"Message {message_id} marked as read")

async def send_location(
self, data: dict, data_location: dict, additional_data: Optional[dict] = {}
self, data: Dict, data_location: Dict, additional_data: Optional[Dict] = {}
) -> Dict[str, str]:
"""
Send a location to a user.
Expand Down
1 change: 1 addition & 0 deletions gupshup_matrix/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ async def handle_matrix_message(

resp = await self.gsc.send_message(
data=gupshup_data,
msgtype=MessageType.TEXT,
body=text,
is_gupshup_template=is_gupshup_template,
additional_data=additional_data,
Expand Down
Loading