refactor(pairing): centralize reply formatting
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
|||||||
listProviderPairingRequests,
|
listProviderPairingRequests,
|
||||||
type PairingProvider,
|
type PairingProvider,
|
||||||
} from "../pairing/pairing-store.js";
|
} from "../pairing/pairing-store.js";
|
||||||
|
import { PROVIDER_ID_LABELS } from "../pairing/pairing-labels.js";
|
||||||
import { sendMessageSignal } from "../signal/send.js";
|
import { sendMessageSignal } from "../signal/send.js";
|
||||||
import { sendMessageSlack } from "../slack/send.js";
|
import { sendMessageSlack } from "../slack/send.js";
|
||||||
import { sendMessageTelegram } from "../telegram/send.js";
|
import { sendMessageTelegram } from "../telegram/send.js";
|
||||||
@@ -22,15 +23,6 @@ const PROVIDERS: PairingProvider[] = [
|
|||||||
"whatsapp",
|
"whatsapp",
|
||||||
];
|
];
|
||||||
|
|
||||||
const PROVIDER_ID_LABELS: Record<PairingProvider, string> = {
|
|
||||||
telegram: "telegramUserId",
|
|
||||||
discord: "discordUserId",
|
|
||||||
slack: "slackUserId",
|
|
||||||
signal: "signalNumber",
|
|
||||||
imessage: "imessageSenderId",
|
|
||||||
whatsapp: "whatsappSenderId",
|
|
||||||
};
|
|
||||||
|
|
||||||
function parseProvider(raw: unknown): PairingProvider {
|
function parseProvider(raw: unknown): PairingProvider {
|
||||||
const value = (
|
const value = (
|
||||||
typeof raw === "string"
|
typeof raw === "string"
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ import {
|
|||||||
readProviderAllowFromStore,
|
readProviderAllowFromStore,
|
||||||
upsertProviderPairingRequest,
|
upsertProviderPairingRequest,
|
||||||
} from "../pairing/pairing-store.js";
|
} from "../pairing/pairing-store.js";
|
||||||
|
import { buildPairingReply } from "../pairing/pairing-messages.js";
|
||||||
import {
|
import {
|
||||||
buildAgentSessionKey,
|
buildAgentSessionKey,
|
||||||
resolveAgentRoute,
|
resolveAgentRoute,
|
||||||
@@ -591,16 +592,11 @@ export function createDiscordMessageHandler(params: {
|
|||||||
try {
|
try {
|
||||||
await sendMessageDiscord(
|
await sendMessageDiscord(
|
||||||
`user:${author.id}`,
|
`user:${author.id}`,
|
||||||
[
|
buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "discord",
|
||||||
"",
|
idLine: `Your Discord user id: ${author.id}`,
|
||||||
`Your Discord user id: ${author.id}`,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider discord <code>",
|
|
||||||
].join("\n"),
|
|
||||||
{ token, rest: client.rest, accountId },
|
{ token, rest: client.rest, accountId },
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -1435,16 +1431,11 @@ function createDiscordNativeCommand(params: {
|
|||||||
});
|
});
|
||||||
if (created) {
|
if (created) {
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content: [
|
content: buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "discord",
|
||||||
"",
|
idLine: `Your Discord user id: ${user.id}`,
|
||||||
`Your Discord user id: ${user.id}`,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider discord <code>",
|
|
||||||
].join("\n"),
|
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
readProviderAllowFromStore,
|
readProviderAllowFromStore,
|
||||||
upsertProviderPairingRequest,
|
upsertProviderPairingRequest,
|
||||||
} from "../pairing/pairing-store.js";
|
} from "../pairing/pairing-store.js";
|
||||||
|
import { buildPairingReply } from "../pairing/pairing-messages.js";
|
||||||
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
||||||
import type { RuntimeEnv } from "../runtime.js";
|
import type { RuntimeEnv } from "../runtime.js";
|
||||||
import { resolveIMessageAccount } from "./accounts.js";
|
import { resolveIMessageAccount } from "./accounts.js";
|
||||||
@@ -256,16 +257,11 @@ export async function monitorIMessageProvider(
|
|||||||
try {
|
try {
|
||||||
await sendMessageIMessage(
|
await sendMessageIMessage(
|
||||||
sender,
|
sender,
|
||||||
[
|
buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "imessage",
|
||||||
"",
|
idLine: `Your iMessage sender id: ${senderId}`,
|
||||||
`Your iMessage sender id: ${senderId}`,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider imessage <code>",
|
|
||||||
].join("\n"),
|
|
||||||
{
|
{
|
||||||
client,
|
client,
|
||||||
maxBytes: mediaMaxBytes,
|
maxBytes: mediaMaxBytes,
|
||||||
|
|||||||
10
src/pairing/pairing-labels.ts
Normal file
10
src/pairing/pairing-labels.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import type { PairingProvider } from "./pairing-store.js";
|
||||||
|
|
||||||
|
export const PROVIDER_ID_LABELS: Record<PairingProvider, string> = {
|
||||||
|
telegram: "telegramUserId",
|
||||||
|
discord: "discordUserId",
|
||||||
|
slack: "slackUserId",
|
||||||
|
signal: "signalNumber",
|
||||||
|
imessage: "imessageSenderId",
|
||||||
|
whatsapp: "whatsappSenderId",
|
||||||
|
};
|
||||||
44
src/pairing/pairing-messages.test.ts
Normal file
44
src/pairing/pairing-messages.test.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { buildPairingReply } from "./pairing-messages.js";
|
||||||
|
|
||||||
|
describe("buildPairingReply", () => {
|
||||||
|
const cases = [
|
||||||
|
{
|
||||||
|
provider: "discord",
|
||||||
|
idLine: "Your Discord user id: 1",
|
||||||
|
code: "ABC123",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provider: "slack",
|
||||||
|
idLine: "Your Slack user id: U1",
|
||||||
|
code: "DEF456",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provider: "signal",
|
||||||
|
idLine: "Your Signal number: +15550001111",
|
||||||
|
code: "GHI789",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provider: "imessage",
|
||||||
|
idLine: "Your iMessage sender id: +15550002222",
|
||||||
|
code: "JKL012",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provider: "whatsapp",
|
||||||
|
idLine: "Your WhatsApp sender id: +15550003333",
|
||||||
|
code: "MNO345",
|
||||||
|
},
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
for (const testCase of cases) {
|
||||||
|
it(`formats pairing reply for ${testCase.provider}`, () => {
|
||||||
|
const text = buildPairingReply(testCase);
|
||||||
|
expect(text).toContain(testCase.idLine);
|
||||||
|
expect(text).toContain(`Pairing code: ${testCase.code}`);
|
||||||
|
expect(text).toContain(
|
||||||
|
`clawdbot pairing approve --provider ${testCase.provider} <code>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
19
src/pairing/pairing-messages.ts
Normal file
19
src/pairing/pairing-messages.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import type { PairingProvider } from "./pairing-store.js";
|
||||||
|
|
||||||
|
export function buildPairingReply(params: {
|
||||||
|
provider: PairingProvider;
|
||||||
|
idLine: string;
|
||||||
|
code: string;
|
||||||
|
}): string {
|
||||||
|
const { provider, idLine, code } = params;
|
||||||
|
return [
|
||||||
|
"Clawdbot: access not configured.",
|
||||||
|
"",
|
||||||
|
idLine,
|
||||||
|
"",
|
||||||
|
`Pairing code: ${code}`,
|
||||||
|
"",
|
||||||
|
"Ask the bot owner to approve with:",
|
||||||
|
`clawdbot pairing approve --provider ${provider} <code>`,
|
||||||
|
].join("\n");
|
||||||
|
}
|
||||||
@@ -53,6 +53,13 @@ export function formatSignalSenderDisplay(sender: SignalSender): string {
|
|||||||
return sender.kind === "phone" ? sender.e164 : `uuid:${sender.raw}`;
|
return sender.kind === "phone" ? sender.e164 : `uuid:${sender.raw}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatSignalPairingIdLine(sender: SignalSender): string {
|
||||||
|
if (sender.kind === "phone") {
|
||||||
|
return `Your Signal number: ${sender.e164}`;
|
||||||
|
}
|
||||||
|
return `Your Signal sender id: ${formatSignalSenderId(sender)}`;
|
||||||
|
}
|
||||||
|
|
||||||
export function resolveSignalRecipient(sender: SignalSender): string {
|
export function resolveSignalRecipient(sender: SignalSender): string {
|
||||||
return sender.kind === "phone" ? sender.e164 : sender.raw;
|
return sender.kind === "phone" ? sender.e164 : sender.raw;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,11 +22,13 @@ import { spawnSignalDaemon } from "./daemon.js";
|
|||||||
import {
|
import {
|
||||||
formatSignalSenderDisplay,
|
formatSignalSenderDisplay,
|
||||||
formatSignalSenderId,
|
formatSignalSenderId,
|
||||||
|
formatSignalPairingIdLine,
|
||||||
isSignalSenderAllowed,
|
isSignalSenderAllowed,
|
||||||
resolveSignalPeerId,
|
resolveSignalPeerId,
|
||||||
resolveSignalRecipient,
|
resolveSignalRecipient,
|
||||||
resolveSignalSender,
|
resolveSignalSender,
|
||||||
} from "./identity.js";
|
} from "./identity.js";
|
||||||
|
import { buildPairingReply } from "../pairing/pairing-messages.js";
|
||||||
import { sendMessageSignal } from "./send.js";
|
import { sendMessageSignal } from "./send.js";
|
||||||
import { runSignalSseLoop } from "./sse-reconnect.js";
|
import { runSignalSseLoop } from "./sse-reconnect.js";
|
||||||
|
|
||||||
@@ -317,11 +319,8 @@ export async function monitorSignalProvider(
|
|||||||
const senderRecipient = resolveSignalRecipient(sender);
|
const senderRecipient = resolveSignalRecipient(sender);
|
||||||
const senderPeerId = resolveSignalPeerId(sender);
|
const senderPeerId = resolveSignalPeerId(sender);
|
||||||
const senderAllowId = formatSignalSenderId(sender);
|
const senderAllowId = formatSignalSenderId(sender);
|
||||||
const senderIdLine =
|
|
||||||
sender.kind === "phone"
|
|
||||||
? `Your Signal number: ${sender.e164}`
|
|
||||||
: `Your Signal sender id: ${senderAllowId}`;
|
|
||||||
if (!senderRecipient) return;
|
if (!senderRecipient) return;
|
||||||
|
const senderIdLine = formatSignalPairingIdLine(sender);
|
||||||
const groupId = dataMessage.groupInfo?.groupId ?? undefined;
|
const groupId = dataMessage.groupInfo?.groupId ?? undefined;
|
||||||
const groupName = dataMessage.groupInfo?.groupName ?? undefined;
|
const groupName = dataMessage.groupInfo?.groupName ?? undefined;
|
||||||
const isGroup = Boolean(groupId);
|
const isGroup = Boolean(groupId);
|
||||||
@@ -352,16 +351,11 @@ export async function monitorSignalProvider(
|
|||||||
try {
|
try {
|
||||||
await sendMessageSignal(
|
await sendMessageSignal(
|
||||||
`signal:${senderRecipient}`,
|
`signal:${senderRecipient}`,
|
||||||
[
|
buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "signal",
|
||||||
"",
|
idLine: senderIdLine,
|
||||||
senderIdLine,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider signal <code>",
|
|
||||||
].join("\n"),
|
|
||||||
{
|
{
|
||||||
baseUrl,
|
baseUrl,
|
||||||
account,
|
account,
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import {
|
|||||||
readProviderAllowFromStore,
|
readProviderAllowFromStore,
|
||||||
upsertProviderPairingRequest,
|
upsertProviderPairingRequest,
|
||||||
} from "../pairing/pairing-store.js";
|
} from "../pairing/pairing-store.js";
|
||||||
|
import { buildPairingReply } from "../pairing/pairing-messages.js";
|
||||||
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
||||||
import { resolveThreadSessionKeys } from "../routing/session-key.js";
|
import { resolveThreadSessionKeys } from "../routing/session-key.js";
|
||||||
import type { RuntimeEnv } from "../runtime.js";
|
import type { RuntimeEnv } from "../runtime.js";
|
||||||
@@ -824,16 +825,11 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
|||||||
try {
|
try {
|
||||||
await sendMessageSlack(
|
await sendMessageSlack(
|
||||||
message.channel,
|
message.channel,
|
||||||
[
|
buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "slack",
|
||||||
"",
|
idLine: `Your Slack user id: ${directUserId}`,
|
||||||
`Your Slack user id: ${directUserId}`,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider slack <code>",
|
|
||||||
].join("\n"),
|
|
||||||
{
|
{
|
||||||
token: botToken,
|
token: botToken,
|
||||||
client: app.client,
|
client: app.client,
|
||||||
@@ -1719,16 +1715,11 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
|||||||
});
|
});
|
||||||
if (created) {
|
if (created) {
|
||||||
await respond({
|
await respond({
|
||||||
text: [
|
text: buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "slack",
|
||||||
"",
|
idLine: `Your Slack user id: ${command.user_id}`,
|
||||||
`Your Slack user id: ${command.user_id}`,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider slack <code>",
|
|
||||||
].join("\n"),
|
|
||||||
response_type: "ephemeral",
|
response_type: "ephemeral",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
readProviderAllowFromStore,
|
readProviderAllowFromStore,
|
||||||
upsertProviderPairingRequest,
|
upsertProviderPairingRequest,
|
||||||
} from "../pairing/pairing-store.js";
|
} from "../pairing/pairing-store.js";
|
||||||
|
import { buildPairingReply } from "../pairing/pairing-messages.js";
|
||||||
import {
|
import {
|
||||||
formatLocationText,
|
formatLocationText,
|
||||||
type NormalizedLocation,
|
type NormalizedLocation,
|
||||||
@@ -310,16 +311,11 @@ export async function monitorWebInbox(options: {
|
|||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
await sock.sendMessage(remoteJid, {
|
await sock.sendMessage(remoteJid, {
|
||||||
text: [
|
text: buildPairingReply({
|
||||||
"Clawdbot: access not configured.",
|
provider: "whatsapp",
|
||||||
"",
|
idLine: `Your WhatsApp sender id: ${candidate}`,
|
||||||
`Your WhatsApp sender id: ${candidate}`,
|
code,
|
||||||
"",
|
}),
|
||||||
`Pairing code: ${code}`,
|
|
||||||
"",
|
|
||||||
"Ask the bot owner to approve with:",
|
|
||||||
"clawdbot pairing approve --provider whatsapp <code>",
|
|
||||||
].join("\n"),
|
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logVerbose(
|
logVerbose(
|
||||||
|
|||||||
Reference in New Issue
Block a user