sendToChat
let promise = window.webxdc.sendToChat(message);
sendToChat()
allows a webxdc app to prepare a message
that can then be sent to a chat by the user.
Implementations may ask the user for a destination chat
and then set up the message as a draft,
so it is clear that the outgoing message is a result of some user interaction.
message
is an object with file
, text
or both set:
-
message.file
: file to be sent, setname
and one ofblob
,base64
orplainText
:message.file.name
: name of the file, including extensionmessage.file.blob
: JavaScriptBlob
, also accepts inherit types likeFile
message.file.base64
: base64 encoded datamessage.file.plainText
: text for textfile, will be encoded as utf-8
-
message.text
: message text to be sent
The text can usually be modified by the user and the user may decide to send the text without the file or abort sending at all. These user decisions are not reported back to the webxdc app.
To let the user focus on sending the message, calling this function may pass control back to the main app and exit the webxdc app. The promise may or may not be resolved before exit.
In case of errors, the app will not exit and the promise will be rejected.
Example:
window.webxdc.sendToChat({
file: {base64: "aGVsbG8=", name: "hello.txt"},
text: "This file was generated by GreatApp"
}).catch((error) => {
console.log(error);
});
Notes:
-
To send and empty file, set an empty string or blob as data. Not setting any data is an error. This is also important for messenger implementors, that need to check for eg.
typeof message.file.base64 === "string"
and not!message.file.base64
, which would not allow empty files. -
If you want to send text don't use
btoa()
, rather usemessage.file.plainText
directly, becausebtoa()
has problems with some unicode/emojis