Skip to content

Commit

Permalink
feat: add support for images, audio, files and locations (receive)
Browse files Browse the repository at this point in the history
  • Loading branch information
alejamp committed Sep 21, 2024
1 parent f9f88a1 commit 20c747f
Show file tree
Hide file tree
Showing 8 changed files with 628 additions and 37 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ The `WootConnector` requires the following environment variables to be set:

Run your Cel.ai assistant, then a new Chatwoot bot called "Bot Name" will be created in your Chatwoot instance. Assign the bot to any Inbox you want to use it with.

## Implemented Features

| | RECEIVE | SEND |
|---------------------|:-------:|:-----:|
| **Text** |||
| **Image** |||
| **Audio** |||
| **Files** |||
| **Custom Attributes** |||
| **Video** |||
| **Location** |||
| **Buttons** | - ||
| **Templates** | - ||

## Roadmap

Expand Down
87 changes: 55 additions & 32 deletions celai_chatwoot/connector/model/woot_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
LocationAttachment,\
MessageAttachmentType

class WootLocationAttachment(LocationAttachment):
def __init__(self, **kwargs):
super().__init__(**kwargs)


class WootAttachment(FileAttachment):

Expand Down Expand Up @@ -59,18 +63,14 @@ async def load_from_message(cls, message: dict) -> list:


# Sample message with image attachment from Chatwoot
# { "attachments": [
# {
# "id": 11529,
# "message_id": 436117,
# "file_type": "image",
# "account_id": 8,
# "extension": null,
# "data_url": "https://chatwoot.celai.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbG91IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1a8d1d96c0630370245a7f741b16990c82c4e3bb/ale1.jpg",
# "thumb_url": "https://chatwoot.celai.com/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbG91IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1a8d1d96c0630370245a7f741b16990c82c4e3bb/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJYW5CbkJqb0dSVlE2RTNKbGMybDZaVjkwYjE5bWFXeHNXd2RwQWZvdyIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--27ede0471899a6e40b40ba7e4525adebc0ea198f/ale1.jpg",
# "file_size": 160813
# }
# ],}
# "id": 11529,
# "message_id": 436117,
# "file_type": "image",
# "account_id": 8,
# "extension": null,
# "data_url": "https://chatwoot.celai.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbG91IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1a8d1d96c0630370245a7f741b16990c82c4e3bb/ale1.jpg",
# "thumb_url": "https://chatwoot.celai.com/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbG91IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1a8d1d96c0630370245a7f741b16990c82c4e3bb/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJYW5CbkJqb0dSVlE2RTNKbGMybDZaVjkwYjE5bWFXeHNXd2RwQWZvdyIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--27ede0471899a6e40b40ba7e4525adebc0ea198f/ale1.jpg",
# "file_size": 160813
@classmethod
async def load_image_from_message(cls, attach: dict):

Expand All @@ -86,41 +86,64 @@ async def load_image_from_message(cls, attach: dict):


# Sample audio message from Chatwoot
# {
# ...
# "attachments": [
# {
# "id": 207,
# "message_id": 2467,
# "file_type": "audio",
# "account_id": 4,
# "extension": null,
# "data_url": "https://chatwoot.com/rails/asd.oga"
# "thumb_url": "",
# "file_size": 7989
# }
# ]
# }
# "id": 207,
# "message_id": 2467,
# "file_type": "audio",
# "account_id": 4,
# "extension": null,
# "data_url": "https://chatwoot.com/rails/asd.oga"
# "thumb_url": "",
# "file_size": 7989
@classmethod
async def load_audio_from_message(cls, attach: dict):
filename = attach["data_url"].split("/")[-1]
return WootAttachment(
title="Audio",
title=filename,
description="Audio attachment",
mimeType="audio",
mimeType=mimetypes.guess_type(attach["data_url"])[0],
metadata=attach,
file_url=attach["data_url"],
type=MessageAttachmentType.AUDIO
)



# Sample location message from Chatwoot
# "id": 15184,
# "message_id": 595490,
# "file_type": "location",
# "account_id": 8,
# "coordinates_lat": -34.574992,
# "coordinates_long": -58.502302,
# "fallback_title": null,
# "data_url": null
@classmethod
async def load_location_from_message(cls, attach: dict):
raise NotImplementedError("Location attachment not implemented")
return WootLocationAttachment(
latitude=attach["coordinates_lat"],
longitude=attach["coordinates_long"],
metadata=attach,
description="Location attachment")

# "id": 15185,
# "message_id": 595520,
# "file_type": "file",
# "account_id": 8,
# "extension": null,
# "data_url": "https://chatwoot.celai.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcGs4IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--67392c158ee9d2e382c576d70eaba10ca95c786e/file_2.pdf",
# "thumb_url": "",
# "file_size": 18810
@classmethod
async def load_file_from_message(cls, attach: dict):
raise NotImplementedError("File attachment not implemented")
filename = attach["data_url"].split("/")[-1]
return WootAttachment(
title=filename,
description="File attachment",
mimeType=mimetypes.guess_type(attach["data_url"])[0],
metadata=attach,
file_url=attach["data_url"],
type=MessageAttachmentType.DOCUMENT
)


@classmethod
async def load_video_from_message(cls, attach: dict):
Expand Down
6 changes: 5 additions & 1 deletion celai_chatwoot/connector/woo_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ async def __process_message(self, payload: dict):

try:
log.debug("Received Chatwoot request")
log.debug(payload)
# log.debug(payload)

if payload.get("message_type", "outgoing") == "outgoing":
log.debug("Ignoring outgoing message")
return

msg = await WootMessage.load_from_message(payload, connector=self)

has_attachments = msg.attachments is not None and len(msg.attachments) > 0
if has_attachments:
log.debug(f"Received message with attachments: {msg.attachments}")

assert isinstance(msg, WootMessage), "msg must be an instance of WootMessage"


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "celai-chatwoot"
version = "0.1.2"
version = "0.1.3"
description = ""
authors = ["Alex Martin <[email protected]>"]
readme = "README.md"
Expand Down
152 changes: 152 additions & 0 deletions tests/data/incoming_audio_msg_tg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"account": {
"id": 8,
"name": "Demos"
},
"additional_attributes": {},
"content_attributes": {},
"content_type": "text",
"content": null,
"conversation": {
"additional_attributes": {
"chat_id": 1320141990
},
"can_reply": true,
"channel": "Channel::Telegram",
"contact_inbox": {
"id": 6981,
"contact_id": 6849,
"inbox_id": 216,
"source_id": "1320141990",
"created_at": "2024-09-21T04:41:45.211Z",
"updated_at": "2024-09-21T04:41:45.211Z",
"hmac_verified": false,
"pubsub_token": "d4EAQrREKUcMtTJCcmcTiEn5"
},
"id": 35,
"inbox_id": 216,
"messages": [
{
"id": 595489,
"content": null,
"account_id": 8,
"inbox_id": 216,
"conversation_id": 35,
"message_type": 0,
"created_at": 1726894319,
"updated_at": "2024-09-21T04:51:59.084Z",
"private": false,
"status": "sent",
"source_id": "8",
"content_type": "text",
"content_attributes": {},
"sender_type": "Contact",
"sender_id": 6849,
"external_source_ids": {},
"additional_attributes": {},
"processed_message_content": null,
"conversation": {
"assignee_id": null,
"unread_count": 1,
"last_activity_at": 1726894319
},
"attachments": [
{
"id": 15183,
"message_id": 595489,
"file_type": "audio",
"account_id": 8,
"extension": null,
"data_url": "https://chatwoot.celai.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcGc4IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--ced19bdedff4a7da3eee55e466fe5bc1506d0832/file_1.oga",
"thumb_url": "",
"file_size": 4062
}
],
"sender": {
"additional_attributes": {
"username": "cowbe1985",
"language_code": "en"
},
"custom_attributes": {},
"email": null,
"id": 6849,
"identifier": null,
"name": "AMPer ",
"phone_number": null,
"thumbnail": "",
"type": "contact"
}
}
],
"labels": [],
"meta": {
"sender": {
"additional_attributes": {
"username": "cowbe1985",
"language_code": "en"
},
"custom_attributes": {},
"email": null,
"id": 6849,
"identifier": null,
"name": "AMPer ",
"phone_number": null,
"thumbnail": "",
"type": "contact"
},
"assignee": null,
"team": null,
"hmac_verified": false
},
"status": "open",
"custom_attributes": {},
"snoozed_until": null,
"unread_count": 1,
"first_reply_created_at": "2024-09-21T04:42:30.327Z",
"priority": null,
"agent_last_seen_at": 1726894291,
"contact_last_seen_at": 0,
"timestamp": 1726894319,
"created_at": 1726893705
},
"created_at": "2024-09-21T04:51:59.084Z",
"id": 595489,
"inbox": {
"id": 216,
"name": "Test Bot"
},
"message_type": "incoming",
"private": false,
"sender": {
"account": {
"id": 8,
"name": "Demos"
},
"additional_attributes": {
"username": "cowbe1985",
"language_code": "en"
},
"avatar": "",
"custom_attributes": {},
"email": null,
"id": 6849,
"identifier": null,
"name": "AMPer ",
"phone_number": null,
"thumbnail": ""
},
"source_id": "8",
"attachments": [
{
"id": 15183,
"message_id": 595489,
"file_type": "audio",
"account_id": 8,
"extension": null,
"data_url": "https://chatwoot.celai.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcGc4IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--ced19bdedff4a7da3eee55e466fe5bc1506d0832/file_1.oga",
"thumb_url": "",
"file_size": 4062
}
],
"event": "message_created"
}
Loading

0 comments on commit 20c747f

Please sign in to comment.