Messages
We have 2 APIs for messages. One for sending the message and one for retrieving the messages.
Send message
Sends a message to the user if the user_id is provided. Otherwise, it will send the message to the admin. You can also send attachments with your message and they will be stored in private storage. Only you and the recipient can view the attachments.
It does not require you 2 to be friends. You can chat with anyone in the social network without being friends. The other user will receive a notification of new message.
URL = http://localhost:8000/api/messages/send
Method = POST
Headers
Accept | application/json |
Authorization | Bearer {access_token} |
Parameters
Key | Type | Required |
---|---|---|
message | string | No |
user_id | integer | No |
time_zone | string (e.g. Asia/Karachi) | No |
attachments[] | files(s) | No |
- message: The message can be empty in case if user wants to send only the attachments.
- user_id: Tells the user whom you want to send a message. If the “user_id” is not specified, then the message will be sent to the admin.
- attachments[]: It will be an array of attachments. All files will be saved in private storage.
Response
Status = 200
{
"status": "success",
"message": "Message has been sent.",
"message_obj": {
"id": 5,
"message": "Hi",
"sender_id": 2,
"receiver_id": 3,
"attachments": [
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..."
],
"created_at": "05 Oct, 2024 10:38:44 pm",
"updated_at": "05 Oct, 2024 10:38:44 pm"
}
}
- attachments: It will be an array containing complete base64 string of attached files.
Since message attachments are being stored in private storage, so we are returning base64 string of file content instead of absolute path.
Fetch messages
Returns your chat with the user (if user_id is provided) or with admin (otherwise). It will also return attachments of each message (if any). The attachments will be in base64 string. Since we are saving attachments in private storage, that’s why generating a relative path will be in-secure.
URL = http://localhost:8000/api/messages/fetch
Method = POST
Headers
Accept | application/json |
Authorization | Bearer {access_token} |
Parameters
Key | Type | Required |
---|---|---|
page | integer (e.g. 1) | Yes |
user_id | integer | No |
time_zone | string (e.g. Asia/Karachi) | No |
- user_id: Tells the user of whom chat you want to fetch. If the “user_id” is not specified, then your chat with the admin will be returned.
Response
Status = 200
{
"status": "success",
"message": "Data has been fetched.",
"messages": [
{
"id": 6,
"message": "Hi",
"sender_id": 2,
"receiver_id": 3,
"attachments": [
{
"path": "data:image/jpeg;base64,/9j/4AAQSkZJRgAB...",
"name": "428672394.jpg",
"type": "image/jpeg"
}
],
"created_at": "05 Oct, 2024 10:40:15 pm"
}
],
"notifications_count": 0
}