Compare commits
3 Commits
ace4e39476
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 02354b3a7c | |||
| 709ef9885b | |||
| 001173037e |
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
conf.js
|
conf.js
|
||||||
last_ts
|
last_ts
|
||||||
Vendored
+2
-2
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"deno.enable": true
|
"deno.enable": true
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ create conf.js and paste values
|
|||||||
export const ACCESS_TOKEN = ""; // your access token you can get it from element settings
|
export const ACCESS_TOKEN = ""; // your access token you can get it from element settings
|
||||||
export const SERVER_URL = ""; // your matrix server url without trailing slash i.e. https://matrix-client.matrix.org
|
export const SERVER_URL = ""; // your matrix server url without trailing slash i.e. https://matrix-client.matrix.org
|
||||||
export const ROOM_ID = ""; // make a private room and put its id here
|
export const ROOM_ID = ""; // make a private room and put its id here
|
||||||
|
export const INTERVAL = 30; // how often to check for new notifications
|
||||||
```
|
```
|
||||||
|
|
||||||
run
|
run
|
||||||
|
|||||||
@@ -1,87 +1,97 @@
|
|||||||
import { ACCESS_TOKEN, SERVER_URL, ROOM_ID } from "./conf.js";
|
import { ACCESS_TOKEN, SERVER_URL, ROOM_ID, INTERVAL } from "./conf.js";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var last_ts = Number(Deno.readTextFileSync("last_ts"));
|
var last_ts = Number(Deno.readTextFileSync("last_ts"));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
var last_ts = 0;
|
var last_ts = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function fetchMatrix(url, options = {}) {
|
async function fetchMatrix(url, options = {}) {
|
||||||
options.headers ||= {};
|
options.headers ||= {};
|
||||||
options.headers.Authorization ||= `Bearer ${ACCESS_TOKEN}`;
|
options.headers.Authorization ||= `Bearer ${ACCESS_TOKEN}`;
|
||||||
url = SERVER_URL + url;
|
if (!(url instanceof URL)) url = new URL(url, SERVER_URL);
|
||||||
do {
|
console.debug("fetch", url.toString());
|
||||||
console.debug("fetch", url);
|
var res = await fetch(url, options);
|
||||||
try {
|
console.debug("status:", res.status);
|
||||||
var res = await fetch(url, options);
|
if (res.status == 429) {
|
||||||
if (res.ok) break;
|
var {retry_after_ms} = await res.json();
|
||||||
console.warn("status:", res.status);
|
await new Promise(r => setTimeout(r, retry_after_ms || 60000));
|
||||||
if (res.status == 429) {
|
return await fetchMatrix(url, options);
|
||||||
var {retry_after_ms} = await res.json();
|
}
|
||||||
}
|
if (!res.ok) throw new Error(`HTTP ${res.status} ${await res.text()}`);
|
||||||
} catch (error) {
|
return res;
|
||||||
console.error(error);
|
}
|
||||||
}
|
|
||||||
await new Promise(r => setTimeout(r, retry_after_ms || 60000));
|
async function checkNotifications() {
|
||||||
} while (true)
|
var new_notifications = [];
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status} ${await res.text()}`);
|
var next_token;
|
||||||
return res;
|
var limit = 5;
|
||||||
}
|
|
||||||
|
l: do {
|
||||||
async function checkNotifications() {
|
var data = await fetchMatrix(`/_matrix/client/v3/notifications?limit=${limit}${next_token ? `&from=${next_token}` : ''}`).then(res => res.json());
|
||||||
var new_notifications = [];
|
for (let n of data.notifications) {
|
||||||
var next_token;
|
if (n.ts <= last_ts) break l;
|
||||||
var limit = 5;
|
new_notifications.unshift(n);
|
||||||
|
}
|
||||||
l: do {
|
next_token = data.next_token;
|
||||||
var data = await fetchMatrix(`/_matrix/client/v3/notifications?limit=${limit}${next_token ? `&from=${next_token}` : ''}`).then(res => res.json());
|
limit = 50;
|
||||||
for (let n of data.notifications) {
|
} while (next_token !== null)
|
||||||
if (n.ts <= last_ts) break l;
|
|
||||||
new_notifications.unshift(n);
|
if (new_notifications.length) console.debug("new notifications:", new_notifications);
|
||||||
}
|
if (!new_notifications.length) return;
|
||||||
next_token = data.next_token;
|
for (let n of new_notifications) {
|
||||||
limit = 50;
|
try {
|
||||||
} while (next_token !== null)
|
await addNotificationToRoom(n);
|
||||||
|
last_ts = n.ts;
|
||||||
console.debug("new notifications:", new_notifications);
|
Deno.writeTextFileSync("last_ts", String(last_ts));
|
||||||
if (!new_notifications.length) return;
|
} catch (error) {
|
||||||
for (let n of new_notifications) {
|
console.error(error);
|
||||||
try {
|
}
|
||||||
await addNotificationToRoom(n);
|
}
|
||||||
last_ts = n.ts;
|
}
|
||||||
Deno.writeTextFileSync("last_ts", String(last_ts));
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
async function addNotificationToRoom({room_id, event}) {
|
||||||
}
|
var url = `https://matrix.to/#/${encodeURIComponent(room_id)}/${encodeURIComponent(event.event_id)}`;
|
||||||
}
|
var text = `${new Date(event.origin_server_ts).toLocaleString()}
|
||||||
}
|
${await getAlias(room_id)}
|
||||||
|
${event.sender}
|
||||||
|
${event.content?.body}
|
||||||
async function addNotificationToRoom(notification) {
|
${url}`;
|
||||||
var text = `https://matrix.to/#/${encodeURIComponent(notification.room_id)}/${encodeURIComponent(notification.event.event_id)} ${new Date(notification.ts).toLocaleString()}`;
|
await fetchMatrix(`/_matrix/client/v3/rooms/${encodeURIComponent(ROOM_ID)}/send/m.room.message/${Math.random()}`, {
|
||||||
await fetchMatrix(`/_matrix/client/v3/rooms/${encodeURIComponent(ROOM_ID)}/send/m.room.message/${Math.random()}`, {
|
method: "PUT",
|
||||||
method: "PUT",
|
headers: {
|
||||||
headers: {
|
"Content-Type": "application/json"
|
||||||
"Content-Type": "application/json"
|
},
|
||||||
},
|
body: JSON.stringify({
|
||||||
body: JSON.stringify({
|
msgtype: "m.text",
|
||||||
msgtype: "m.text",
|
body: text
|
||||||
format: "org.matrix.custom.html",
|
//format: "org.matrix.custom.html",
|
||||||
body: text,
|
//formatted_body: `<p>${text}</p>\n<pre><code class=\"language-json\">${JSON.stringify(event, null, '\t')}\n</code></pre>\n`
|
||||||
formatted_body: `<p>${text}</p>\n<pre><code class=\"language-json\">${JSON.stringify(notification.event, null, '\t')}\n</code></pre>\n`
|
})
|
||||||
})
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
async function getAlias(room_id) {
|
||||||
|
try {
|
||||||
|
var data = await fetchMatrix(`/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/state/m.room.canonical_alias/`).then(res => res.json());
|
||||||
|
console.debug(data);
|
||||||
while (true) {
|
return data.alias || room_id;
|
||||||
try {
|
} catch (error) {
|
||||||
await checkNotifications();
|
console.error(error);
|
||||||
} catch (error) {
|
return room_id;
|
||||||
console.error(error);
|
}
|
||||||
}
|
}
|
||||||
await new Promise(r => setTimeout(r, 10000));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
await checkNotifications();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
await new Promise(r => setTimeout(r, (INTERVAL || 30) * 1000));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user