Example: weather
What ships is the extension/ folder: manifest.json and main.js. The test sits beside it, outside the package. Back to all examples.
extension/manifest.json
{
"id": "com.example.weather",
"name": "Weather Report",
"version": "1.0.0",
"entry": "main.js",
"description": "Adds /weather: looks up the current weather for a place on wttr.in and posts it in the chat.",
"permissions": ["ui.command", "chat.send", "ui.notify"],
"net": ["wttr.in"],
"author": { "name": "Example Author", "key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }
}
extension/main.js
// @ts-check
/// <reference types="@rin-chat/extension-api" />
// Weather Report: rin.net.request from a command. The manifest's `net` lists wttr.in, which only ASKS for
// it: every extension starts Blocked, and the user chooses its network level on its page in Settings >
// Extensions. From a command (user-started code), a request to a host the user has not allowed waits for
// the user's answer in the review dialog, which shows the request exactly as it will be sent.
const WHY = {
net_blocked: "its network access is Blocked; change that on its page in Settings > Extensions",
net_denied: "the request was denied",
net_failed: "wttr.in did not answer",
net_rate_limited: "too many requests, wait a minute",
};
rin.ui.command("weather", "Post the current weather for a place, such as /weather Lisbon");
rin.on("onCommand", async ({ name, args }) => {
if (name !== "weather") return;
const place = args.trim().slice(0, 80);
if (!place) {
await rin.ui.notify("Name a place: /weather Lisbon");
return;
}
let res;
try {
res = await rin.net.request({ method: "GET", url: `https://wttr.in/${encodeURIComponent(place)}?format=3` });
} catch (e) {
const code = e instanceof Error ? e.message : String(e);
await rin.ui.notify(`No weather: ${WHY[code] ?? code}.`);
return;
}
// A 3xx is the response itself: redirects are never followed.
if (res.status !== 200) {
await rin.ui.notify(`No weather: wttr.in answered ${res.status}.`);
return;
}
// One line of text. A post may not carry a web address, so keep only the report itself.
const line = res.body.split("\n")[0].trim().slice(0, 200);
await rin.chat.send(`Weather: ${line}`);
});
weather.test.mjs
Run it from the example's folder: rinext test extension weather.test.mjs.
// rinext test extension weather.test.mjs
import assert from "node:assert/strict";
const server = () => ({ status: 200, body: "Lisbon: sunny +18°C\n" });
export async function blockedUntilTheUserChangesTheLevel({ open, net }) {
// A fresh install starts Blocked: no dialog, nothing sent.
const chat = await open();
await chat.command("/weather Lisbon");
assert.equal(net.sent.length, 0);
assert.match(chat.notices.join("\n"), /network access is Blocked/);
}
export async function askEveryTimeShowsTheRequestFirst({ open, net }) {
net.level = "ask";
const seen = [];
net.review = (plan) => (seen.push(plan), "once");
net.respond = server;
const chat = await open();
await chat.command("/weather Lisbon");
assert.equal(seen.length, 1);
assert.equal(seen[0].url, "https://wttr.in/Lisbon?format=3");
assert.equal(chat.posts[0].text, "Weather: Lisbon: sunny +18°C");
}
export async function aDeniedRequestSendsNothing({ open, net }) {
net.level = "ask";
net.review = () => "deny";
net.respond = server;
const chat = await open();
await chat.command("/weather Lisbon");
assert.equal(net.sent.length, 0);
assert.match(chat.notices.join("\n"), /the request was denied/);
}
export async function alwaysAllowSkipsTheDialogAfterward({ open, net }) {
net.level = "ask";
let asked = 0;
net.review = () => (asked++, "always");
net.respond = server;
const chat = await open();
await chat.command("/weather Lisbon");
await chat.command("/weather Porto");
assert.equal(asked, 1);
assert.equal(net.level, "allowed");
assert.deepEqual(net.sent.map((p) => p.url), ["https://wttr.in/Lisbon?format=3", "https://wttr.in/Porto?format=3"]);
}