Skip to content

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

Control plane push: a bot's message on the bot's own Android channel when the phone has one

AdityaUmale301bec8e20bc
diff --git a/cloudflare/control-plane/README.md b/cloudflare/control-plane/README.md
index 62efed913b..a38761b07a 100644
--- a/cloudflare/control-plane/README.md
+++ b/cloudflare/control-plane/README.md
@@ -216,8 +216,8 @@ shape.
set is `503 push_unavailable`.
- `unregister` `{ "v": 1, "platform"?: …, "token": "…" }` deletes it. `204`.
- `send` `{ "v": 1, "token": "…", "pairing"?: "…", "collapseId"?: "…",
- "notification": { "kind", "title", "botId", "botName", "threadId",
- "roomId"?, "roomName"? } }` pushes to a token **this** installation
+ "botChannel"?: true, "notification": { "kind", "title", "botId", "botName",
+ "threadId", "roomId"?, "roomName"? } }` pushes to a token **this** installation
registered, else `404 push_device_unknown` (the harness then registers and
retries once). The schema is strict: `kind` is `[a-z-]`, ids are
`[\w-]{1,120}`, `title` is one line of at most 200 characters, names at most
@@ -303,7 +303,9 @@ a data-only message the phone's notification library presents itself:
JSON> }, "android": { "priority": "HIGH", "ttl": "86400s" } } }`. There is no
`message` key, so the notification shows its title and nothing else.
`channelId` is the app's `approval_question` for approvals, questions and
-sign-ins, `routine_failed` for routines and `bot_messages` for the rest.
+sign-ins, `routine_failed` for routines and `bot_messages` for the rest, or
+`bot_messages.<botId>`, the bot's own channel, when the send says
+`"botChannel": true` (the phone registered that it has one). APNs ignores it.
Provider answers are mapped for the harness. A dead token is deleted for
every installation that registered it and answers `410
diff --git a/cloudflare/control-plane/src/push.ts b/cloudflare/control-plane/src/push.ts
index d1bcdaa0d8..2d46f20dcc 100644
--- a/cloudflare/control-plane/src/push.ts
+++ b/cloudflare/control-plane/src/push.ts
@@ -102,6 +102,8 @@ export const sendSchema = z.strictObject({
token: z.string().regex(ANY_TOKEN),
pairing: z.string().regex(PAIRING).optional(),
collapseId: z.string().regex(COLLAPSE_ID).optional(),
+ /** Android: the phone has made the bot its own channel (`bot_messages.<botId>`), so its messages can have their own sound. */
+ botChannel: z.literal(true).optional(),
notification: z.strictObject({
kind: z.string().regex(KIND),
title: oneLine(200),
@@ -197,8 +199,10 @@ export function pushCategory(kind: string): string {
return CATEGORY[kind] ?? CATEGORY_UPDATE;
}
-export function pushChannel(kind: string): string {
- return BLOCKING_KINDS.has(kind) ? CHANNEL_BLOCKING : ROUTINE_KINDS.has(kind) ? CHANNEL_ROUTINES : CHANNEL_MESSAGES;
+export function pushChannel(kind: string, botId?: string): string {
+ if (BLOCKING_KINDS.has(kind)) return CHANNEL_BLOCKING;
+ if (ROUTINE_KINDS.has(kind)) return CHANNEL_ROUTINES;
+ return botId ? `${CHANNEL_MESSAGES}.${botId}` : CHANNEL_MESSAGES;
}
/** `body` repeats `omb` because expo-notifications (SDK 58) hands an app only a remote notification's `body` as its data. */
@@ -229,7 +233,7 @@ export function fcmPayload(request: PushSendRequest, token: string, collapseId:
data: {
title: notification.title,
tag: collapseId,
- channelId: pushChannel(notification.kind),
+ channelId: pushChannel(notification.kind, request.botChannel ? notification.botId : undefined),
categoryId: pushCategory(notification.kind),
threadIdentifier: notification.threadId,
body: JSON.stringify(ombPayload(request)),
diff --git a/cloudflare/control-plane/test/push-fcm.test.ts b/cloudflare/control-plane/test/push-fcm.test.ts
index f1861178ea..2dd9eb203d 100644
--- a/cloudflare/control-plane/test/push-fcm.test.ts
+++ b/cloudflare/control-plane/test/push-fcm.test.ts
@@ -204,6 +204,23 @@ describe("phone push relay, FCM", () => {
expect(second.message.data.tag).toMatch(/^[0-9a-f]{32}$/);
});
+ it("puts a bot's message on the bot's own channel when the phone has one, and approvals and routines on theirs", async () => {
+ const account = await serviceAccount();
+ const environment = testEnv({ FCM_SERVICE_ACCOUNT: account.json });
+ const providers = fakeProviders();
+ const worker = createWorker(fetch, providers.fetch);
+ const { credential } = await installation(worker, environment, "[email protected]");
+ await registerFcm(worker, environment, credential);
+ const done = { ...notification, kind: "done", title: "Scout finished" };
+ expect((await send(worker, environment, credential, { botChannel: true, notification: done })).status).toBe(202);
+ expect((await send(worker, environment, credential, { botChannel: true })).status).toBe(202);
+ expect((await send(worker, environment, credential, { botChannel: true, notification: { ...notification, kind: "routine-failed" } })).status).toBe(202);
+ expect((await send(worker, environment, credential, { notification: done })).status).toBe(202);
+ const channels = providers.fcm().map((one) => (JSON.parse(one.text) as { message: { data: Record<string, string> } }).message.data.channelId);
+ expect(channels).toEqual(["bot_messages.bot_1", "approval_question", "routine_failed", "bot_messages"]);
+ expect((await send(worker, environment, credential, { botChannel: false, notification: done })).status).toBe(400);
+ });
+
it("drops a token FCM calls unregistered or foreign, for every installation", async () => {
const account = await serviceAccount();
const environment = testEnv({ FCM_SERVICE_ACCOUNT: account.json });