Messages
Content
When you start the app, you can listen for incoming messages using the run
function.
src/index.ts
run(async (context: XMTPContext) => {
const {
message: {
id,
content: { text, skill, params, attachment },
reply,
reference,
},
sender,
sent,
typeId,
} = context;
/*Logic*/
});
id
: string // The id of the messagecontent
: all the content of the message depending on the typetext
: string // The content of the messageskill
: string, // The skill of the messageparams
: string[], // The parameters of the messageattachment
: Attachment // The attachment of the messagereply
: Reply // The reply of the messagereference
: string // The reference of the message
sender
: User // The user object of the sendersent
: Date // The timestamp of the messagetypeId
: string // The type of the message (text, reply, attachment, skill)
Sending
App messages are messages that are sent when you send a reply to a message and are highlighted differently by the apps.
// Send a message
await context.send("Your message.");
// Reply to the last message
await context.reply("Your message.");
// Send a message to specific users
await context.sendTo("Your message.", ["address1", "address2"]);
// Await a response
const answer = await context.awaitResponse("Please answer with yes or no", [
"yes",
"no",
]);
console.log(`You answered: ${answer}`);
// Send an image
await context.sendImage("path/to/image.png");
// Send a payment url
await context.sendPayment(1, "USDC", address);
//Send receipt
await context.sendReceipt(result.value!, "logo", "base", "token", 1);
Receiving
Attachments are files that are sent with the message. They can be images, videos, or any other file type.
run(async (context: XMTPContext) => {
const {
message: {
id,
content: { reply, text, skill, params, attachment },
reference,
typeId,
},
} = context;
// Check if the message is an attachment
if (typeId === "text") {
// Do something with the text
} else if (typeId === "skill") {
// Do something with the skill
const { address, domain } = params;
// Use params
} else if (typeId === "reply") {
// Do something with the `reply`
} else if (typeId === "attachment") {
const { data, mimeType, filename } = attachment;
const base64Image = Buffer.from(data).toString("base64");
// Get the data URL in base64
const dataUrl = `data:${mimeType};base64,${base64Image}`;
}
});