发送和接收消息
开始通过 REST API 发送消息和检索聊天消息。
// Send text message
const sendResponse = await fetch("https://chatapi.smsbat.com/api/chat/" + chatId + "/message", {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
},
body: JSON.stringify({
textMessage: "Hello from Chathub!",
author: 0
})
});
// Send media message
const mediaResponse = await fetch("https://chatapi.smsbat.com/api/chat/" + chatId + "/message", {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
},
body: JSON.stringify({
textMessage: "Check this image!",
author: 0,
media: {
name: "image.jpg",
format: "image/jpeg",
dataBase64: "base64EncodedString",
type: 1
}
})
});
// Get chat messages
const messagesResponse = await fetch("https://chatapi.smsbat.com/api/chat/" + chatId + "/messages", {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + token
}
});
const messages = await messagesResponse.json();
console.log('Messages:', messages);