Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Phone push: send a bot's push on its own Android channel when the phone registered one

AdityaUmaled16f38560a07
diff --git a/docs/phone-push.md b/docs/phone-push.md
index b18a1ec922..ad0bd29667 100644
--- a/docs/phone-push.md
+++ b/docs/phone-push.md
@@ -68,7 +68,8 @@ person.
third-party push service sits in between.
2. **The phone gives the token to its computer or Cloud,** over the link it
already uses and with the credential it already has:
- `PUT /api/push/device {platform, token, environment?, pairing, kinds}`. The
+ `PUT /api/push/device {platform, token, environment?, pairing, kinds,
+ botChannels?}`. The
server knows which phone is asking from what authenticated the request (the
phone's paired session, or the device the desktop's companion vouched for),
never from the body. The phone does this after it connects, whenever the
@@ -351,6 +352,14 @@ in order (all done; kept as the record and for a new environment):
(high importance) for approvals, questions and sign-ins, and `bot_messages`
for finished replies. A phone that never opened the new app has no channels
yet and shows the push in the default channel.
+- An Android phone can name the bots it made a channel of their own for
+ (`botChannels`, ids). A push for one of those bots then goes out with
+ `botChannel: true`, and the control plane sends a finished reply on
+ `bot_messages.<botId>`, where the person can give the bot its own sound.
+ The server never names a bot the phone did not list, because Android shows
+ a push for a missing channel under "Miscellaneous". A Cloud home leaves the
+ field out, since its Admin's copy of the send schema does not take it yet,
+ so its phones keep the shared `bot_messages` channel.
- The categories (Allow and Deny on an approval, Answer on a question) are the
ones the app registers today.
diff --git a/packages/phone-protocol/src/phone-push.ts b/packages/phone-protocol/src/phone-push.ts
index fe63020e23..5f88143c68 100644
--- a/packages/phone-protocol/src/phone-push.ts
+++ b/packages/phone-protocol/src/phone-push.ts
@@ -32,6 +32,8 @@ export const PAIRING_TAG = /^[A-Za-z0-9_-]{8,64}$/;
export const PUSH_COLLAPSE_ID = /^[A-Za-z0-9._:-]{1,64}$/;
export const PUSH_ID = /^[\w-]{1,120}$/;
export const PUSH_NAME_MAX = 80;
+/** The most bots one registration can name in `botChannels`: a full list of UUID ids stays well inside the 16 KB body. */
+export const PUSH_BOT_CHANNELS_MAX = 200;
export const PUSH_TITLE_MAX = 200;
/** The kinds a closed app is woken for: a bot waits on the person (approval, question, sign-in), or a reply is ready.
@@ -49,6 +51,9 @@ export interface PhonePushRegistration {
/** The kinds this phone wants pushed, from its own notification switches. Absent: all of `PUSH_KINDS`. Empty: none,
* the registration is kept. */
kinds?: PushKind[];
+ /** FCM only: the bots (ids) that have a notification channel of their own on this phone, `bot_messages.<botId>`.
+ * A push for one of them is sent on it, so the person can give that bot its own sound. Absent: none. */
+ botChannels?: string[];
}
/** Whether pushes can leave this server at all: `off` until the owner switches phone push on, `no-account` while it
@@ -86,6 +91,8 @@ export interface PushRelaySendRequest {
token: string;
pairing?: string;
collapseId?: string;
+ /** The phone has a channel of the bot's own: a bot message goes on it instead of the shared one (Android only). */
+ botChannel?: true;
notification: PhonePushNotification;
}
diff --git a/server/phone-push.test.ts b/server/phone-push.test.ts
index 732cb671ce..674d566953 100644
--- a/server/phone-push.test.ts
+++ b/server/phone-push.test.ts
@@ -246,11 +246,38 @@ describe("registering a phone", () => {
{ platform: "apns", token: APNS, pairing: "no spaces allowed" },
{ platform: "apns", token: APNS, pushToken: APNS },
{ platform: "webpush", token: FCM },
+ { platform: "apns", token: APNS, botChannels: ["bot_1"] },
+ { platform: "fcm", token: FCM, botChannels: ["not a bot id"] },
]) expect(registrationSchema.safeParse(body).success, JSON.stringify(body)).toBe(false);
});
});
describe("offering a notify frame", () => {
+ it("tells the relay a bot's push may use the bot's own Android channel, only for bots the phone made one for", async () => {
+ const { push, relay } = harness();
+ await push.register(OTHER, { platform: "fcm", token: FCM, botChannels: ["bot_1", "bot_1"] });
+ push.offer({ notification: notification({ kind: "done", threadId: "th_a" }), seq: 1 }, everyone);
+ push.offer({ notification: notification({ kind: "done", botId: "bot_2", botName: "Atlas", threadId: "th_b" }), seq: 2 }, everyone);
+ await push.flush();
+ expect(relay.sends().map((call) => [call.body.notification, call.body.botChannel])).toEqual([
+ [expect.objectContaining({ botId: "bot_1" }), true],
+ [expect.objectContaining({ botId: "bot_2" }), undefined],
+ ]);
+ await push.register(OTHER, { platform: "fcm", token: FCM });
+ push.offer({ notification: notification({ kind: "done", threadId: "th_c" }), seq: 3 }, everyone);
+ await push.flush();
+ expect(relay.sends().at(-1)?.body).not.toHaveProperty("botChannel");
+ });
+
+ it("leaves the bot's own channel out through a Cloud home's Admin, which takes the earlier send body", async () => {
+ const { push, relay } = harness({ access: () => ({ admin: "https://admin.openmausbot.test", machineId: "machine-1", secret: "s".repeat(43) }) });
+ await push.register(OTHER, { platform: "fcm", token: FCM, botChannels: ["bot_1"] });
+ push.offer({ notification: notification({ kind: "done" }), seq: 1 }, everyone);
+ await push.flush();
+ expect(relay.sends()).toHaveLength(1);
+ expect(relay.sends()[0]?.body).not.toHaveProperty("botChannel");
+ });
+
it("pushes only to a phone with no live stream that would have seen it, content-free, with the stream's identifier", async () => {
const { push, relay, liveKeys } = harness();
await push.register(PHONE, { platform: "apns", token: APNS, pairing: "pair-tag-0001" });
diff --git a/server/phone-push.ts b/server/phone-push.ts
index 50588e5779..6d4437cde4 100644
--- a/server/phone-push.ts
+++ b/server/phone-push.ts
@@ -16,6 +16,7 @@ import {
PAIRING_TAG,
PHONE_PUSH_RELAY_PATHS,
PHONE_PUSH_WIRE_VERSION,
+ PUSH_BOT_CHANNELS_MAX,
PUSH_ID,
PUSH_KINDS,
PUSH_NAME_MAX,
@@ -81,10 +82,11 @@ export function phonePushEnabled(cfg: { features?: { phonePush?: boolean } }, en
}
const kindsSchema = z.array(z.enum(PUSH_KINDS)).max(PUSH_KINDS.length);
+const botChannelsSchema = z.array(z.string().regex(PUSH_ID)).max(PUSH_BOT_CHANNELS_MAX);
export const registrationSchema = z.discriminatedUnion("platform", [
z.object({ platform: z.literal("apns"), token: z.string().trim().toLowerCase().regex(APNS_TOKEN), environment: z.enum(["production", "sandbox"]).default("production"), pairing: z.string().regex(PAIRING_TAG).optional(), kinds: kindsSchema.optional() }).strict(),
- z.object({ platform: z.literal("fcm"), token: z.string().trim().regex(FCM_TOKEN), environment: z.literal("production").optional(), pairing: z.string().regex(PAIRING_TAG).optional(), kinds: kindsSchema.optional() }).strict(),
+ z.object({ platform: z.literal("fcm"), token: z.string().trim().regex(FCM_TOKEN), environment: z.literal("production").optional(), pairing: z.string().regex(PAIRING_TAG).optional(), kinds: kindsSchema.optional(), botChannels: botChannelsSchema.optional() }).strict(),
]);
const storedDevice = z.object({
@@ -94,6 +96,7 @@ const storedDevice = z.object({
environment: z.enum(["production", "sandbox"]),
pairing: z.string().regex(PAIRING_TAG).optional(),
kinds: kindsSchema,
+ botChannels: botChannelsSchema.optional(),
createdAt: z.number(),
updatedAt: z.number(),
}).refine((device) => validPushToken(device.platform, device.token));
@@ -311,6 +314,7 @@ export class PhonePush {
environment: input.platform === "apns" ? input.environment ?? "production" : "production",
...(input.pairing ? { pairing: input.pairing } : {}),
kinds: [...new Set(input.kinds ?? PUSH_KINDS)],
+ ...(input.platform === "fcm" && input.botChannels?.length ? { botChannels: [...new Set(input.botChannels)] } : {}),
createdAt: previous?.createdAt ?? now,
updatedAt: now,
};
@@ -438,7 +442,14 @@ export class PhonePush {
if (this.options.live(job.key)) return "live";
// Near the hour's cap a finished reply gives way, so the asks still get through.
if (!this.roomFor(job.key, job.push.kind === "done" ? "other" : "ask", now)) return "dropped";
- const body = { v: PHONE_PUSH_WIRE_VERSION, token: device.token, ...(device.pairing ? { pairing: device.pairing } : {}), ...(job.collapseId ? { collapseId: job.collapseId } : {}), notification: job.push };
+ const body = {
+ v: PHONE_PUSH_WIRE_VERSION,
+ token: device.token,
+ ...(device.pairing ? { pairing: device.pairing } : {}),
+ ...(job.collapseId ? { collapseId: job.collapseId } : {}),
+ ...(device.botChannels?.includes(job.push.botId) && this.viaInstallation() ? { botChannel: true } : {}),
+ notification: job.push,
+ };
let answer = await this.relay(PHONE_PUSH_RELAY_PATHS.send, body);
if (answer?.status === 404 && answer.code === PUSH_RELAY_ERRORS.unknownDevice) {
const registered = await this.relay(PHONE_PUSH_RELAY_PATHS.register, this.registerBody(device));
@@ -471,6 +482,13 @@ export class PhonePush {
return "paused";
}
+ /** Whether relay calls go to the control plane itself. A Cloud home's Admin checks the send body against its own copy
+ * of the schema, which does not take `botChannel`. */
+ private viaInstallation(): boolean {
+ const access = this.options.access();
+ return !!access && "credential" in access;
+ }
+
/** One POST to the relay; null when it could not be reached. The credential travels only in the Authorization header,
* to the control plane's own origin, and is never logged. A Cloud home's goes to its Admin, signed with its bootstrap
* secret, which never leaves this process: the same body, the same answers. */