How to Send Messages with Attachments
Definition
All the sendTo*
methods can also attach a file. Sending an attachment is done with the attachmentFile
or attachmentFiles
keys. Currently only a single attachment per message is supported, so if using attachmentFiles
array, make sure to send only a single entry.
client.messages.sendToUser(userId: string|Object,body: string,options = {attachmentFile: ?string|?Object,attachmentFiles: ?Array<string|Object>,deleteOnRead : ?boolean,groupMetadata: ?Object,groupName: ?string,metadata : ?Object[]|?ObjectorganizationId: string, // if sender organizationId is different than recipient'spriority : ?string,recipientOrganizationId: ?string, // defaults to senderOrganizationIdsenderOrganizationId: string, // defaults to organizationIdttl : ?number,} : ?Object):Promise<Message,Error>
Parameters
Parameter | Type | Description |
---|---|---|
userId | string |Object | User ID |
body | string |number | Message Body |
options | ?Object | Options (described below) |
Options
Parameter | Type | Description |
---|---|---|
attachmentFile | ?string |?Object | An attachment file. A shortcut to a single item in attachmentFiles . |
attachmentFiles | ?Array<string |Object> | A list of attachments. Currently only a single attachment is supported. |
deleteOnRead | ?boolean | Whether the message should be deleted on read (default: false ) |
groupMetadata | ?Object | Group metadata if sending to a group |
groupName | ?string | Group name if sending to a group |
metadata | ?Object[] |?Object | An array of message metadata objects of extra information on the message. |
organizationId | ?string | Organization ID |
priority | ?string | Message priority. Values: NORMAL (default), HIGH |
recipientOrganizationId | ?string | Defaults to senderOrganizationId |
senderOrganizationId | string | Defaults to organizationId |
ttl | ?number | Time to live in minutes. Organization settings might override this ttl. |
Response
An error will be thrown if any of the arguments are invalid.
Otherwise, once the returned Promise
is resolved, you will have access to the message object being sent
Example
Web
On the Web, attachmentFile
takes an instance of File, which is available on an <input type="file">
files
collection.
let file = inputFileReference.files[0];const message = await client.messages.sendToUser("some-user-id", "hello!", {organizationId: "some-org-id",attachmentFile: file});console.log("sent", message.body, "with attachments", message.attachments);
Node.js
There's a slight difference between the web and the Node.js implementations:
In Node, attachmentFile
can be either an absolute path to a local file (a string), or a Buffer
object, that can be obtained by fs.readFile/fs.readFileSync
.
let file = "/path/to/file.jpg";// orlet file = fs.readFileSync("/path/to/file");const message = await client.messages.sendToUser("some-user-id", "hello!", {organizationId: "some-org-id",attachmentFile: file});console.log("sent", message.body, "with attachments", message.attachments);