Skip to content

Commit

Permalink
35 Message metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
zatziky authored Jan 14, 2020
2 parents 8327097 + bf50298 commit bfe7150
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,19 @@ Returns `true` if the client is successfully connected to Amio Chat server.
### getSessionId()
It returns session ID of the client connected to Amio Chat server. It return `null` if the connection was not successful.

### messages.send(content)
### messages.send(content, metadata)
Sends a message.

Parameters:
- **content** - Message content. See [Amio documentation](https://docs.amio.io/v1.0/reference#messages-send-message) for details about the format.
- **metadata** - Optional. Add [metadata](https://docs.amio.io/v1.0/reference#messages-metadata) to the message. Metadata has to be an object and can carry whatever data needed to be sent along with the message.

```js
amioChat.messages.send({
type: 'text',
payload: 'Hello world'
}, {
any: "arbitrary data"
})
.then(() => {
console.log('Message sent successfully')
Expand All @@ -121,16 +124,18 @@ amioChat.messages.send({
```

### messages.sendText(text)
Sends a text message. This is just a handy shortcut for `messages.send({type: 'text', payload: '...'})`
Sends a text message. This is just a handy shortcut for `messages.send({type: 'text', payload: '...'}, metadata)`

Parameters:
- **text** - The content of the text message.
- **metadata** - Optional. Add [metadata](https://docs.amio.io/v1.0/reference#messages-metadata) to the message. Metadata has to be an object and can carry whatever data needed to be sent along with the message.

### messages.sendImage(url)
### messages.sendImage(url, metadata)
Sends an image message. This is just a handy shortcut for `messages.send({type: 'image', payload: '...'})`

Parameters:
- **url** - The URL of the image.
- **metadata** - Optional. Add [metadata](https://docs.amio.io/v1.0/reference#messages-metadata) to the message. Metadata has to be an object and can carry whatever data needed to be sent along with the message.

### messages.list(nextCursor, max)
Loads messages from message history. Can be called multiple times to move further back in history.
Expand All @@ -155,6 +160,9 @@ Response format:
"content": {
"type": "text",
"payload": "World"
},
"metadata": {
"any": "arbitrary data"
}
},
{
Expand Down Expand Up @@ -259,6 +267,9 @@ Parameters:
"type": "{{MESSAGE TYPE}}",
"payload": "{{MESSAGE PAYLOAD}}"
},
"metadata": {
"any": "arbitrary data"
},
"sent": "{{SENT_TIMESTAMP}}",
"delivered": "{{DELIVERED_TIMESTAMP}}",
"read": "{{READ_TIMESTAMP}}"
Expand All @@ -285,6 +296,9 @@ Parameters:
"type": "{{MESSAGE TYPE}}",
"payload": "{{MESSAGE PAYLOAD}}"
},
"metadata": {
"any": "arbitrary data"
},
"sent": "{{SENT_TIMESTAMP}}",
"delivered": "{{DELIVERED_TIMESTAMP}}",
"read": "{{READ_TIMESTAMP}}"
Expand Down
16 changes: 14 additions & 2 deletions lib/amio-chat-sdk-web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amio-chat-sdk-web.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/amio-chat-sdk-web.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/amio-chat-sdk-web.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amio-chat-sdk-web",
"version": "1.1.0",
"version": "1.2.0",
"description": "Amio Chat JavaScript SDK for custom webchat implementation. https://amio.io",
"main": "lib/amio-chat-sdk-web.js",
"module": "src/amio-chat-client.js",
Expand Down
19 changes: 14 additions & 5 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,50 @@ import {

class Messages {

send(content) {
send(content, metadata = null) {
return new Promise((resolve, reject) => {
if(typeof content !== 'object' || content === null) {
reject('Content is not an object (did you want to use messages.sendText() instead?).')
return
}

if(metadata && typeof metadata !== 'object') {
reject('Metadata must be an object.')
return
}

const data = {
content: content
}

if(metadata) {
data.metadata = metadata
}

connection.emit(SOCKET_MESSAGE_CLIENT, data)
.then(resolve)
.catch(reject)
})
}

sendText(text) {
sendText(text, metadata = null) {
const content = {
type: 'text',
payload: text
}

return this.send(content)
return this.send(content, metadata)
}

sendImage(url) {
sendImage(url, metadata = null) {
const content = {
type: 'image',
payload: {
url: url
}
}

return this.send(content)
return this.send(content, metadata)
}

list(nextCursor, max = 10) {
Expand Down

0 comments on commit bfe7150

Please sign in to comment.