//native
type Ably = {
apiKey: string;
};
/**
* Publish a message to a channel
* Publish a message to the specified channel
*/
export async function main(
auth: Ably,
channel_id: string,
format: "json" | "jsonp" | "msgpack" | "html" | undefined,
X_Ably_Version: string,
body: {
name?: string;
data?: string;
id?: string;
timestamp?: number;
encoding?: string;
clientId?: string;
connectionId?: string;
extras?: {
push?: {
data?: string;
notification?: {
title?: string;
body?: string;
icon?: string;
sound?: string;
collapseKey?: string;
};
apns?: {
notification?: {
title?: string;
body?: string;
icon?: string;
sound?: string;
collapseKey?: string;
};
};
fcm?: {
notification?: {
title?: string;
body?: string;
icon?: string;
sound?: string;
collapseKey?: string;
};
};
web?: {
notification?: {
title?: string;
body?: string;
icon?: string;
sound?: string;
collapseKey?: string;
};
};
};
};
},
) {
const url = new URL(`https://rest.ably.io/channels/${channel_id}/messages`);
for (const [k, v] of [["format", format]]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
headers: {
"X-Ably-Version": X_Ably_Version,
"Content-Type": "application/json",
Authorization: "Bearer " + auth.apiKey,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 138 days ago