Skip to content

Commit

Permalink
support-db
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Nov 1, 2024
1 parent 5f41756 commit a47d7c9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const { inject: injectUrl, access: accessUrl } = context((): string => {

type Medium =
| "whatsapp"
| "instantdb"
| "green-api"
| "telegram"
| "websocket"
Expand Down
23 changes: 23 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { TaskHandler } from "./api.ts";
import type { Endpoint } from "./taskBouncer.ts";
import { makeKey, now, websocketInject } from "./websocket.ts";

type ClientRequest = { from: string; text: string };

export const makeInstantDbHandler = (
storer: <T>(value: T) => Promise<void>,
doTask: TaskHandler,
path: string,
botName: string,
): Endpoint => ({
bounce: true,
method: "POST",
path,
handler: async ({ from, text }: ClientRequest) => {
await storer({ from, key: makeKey(), text, time: now() });
return websocketInject(
(x) => storer({ ...x, time: now(), from: botName }),
from,
)(doTask)({ text });
},
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./api.ts";
export * from "./greenApi.ts";
export * from "./database.ts";
export * from "./taskBouncer.ts";
export * from "./telegram.ts";
export * from "./websocket.ts";
Expand Down
8 changes: 5 additions & 3 deletions src/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const sendTelegramMessage = (token: string) =>
);

export const setTelegramWebhook = (token: string, url: string) =>
fetch(tokenToTelegramURL(token) + `setWebhook?url=${url}`);
fetch(`${tokenToTelegramURL(token)}setWebhook?url=${url}`);

const fileIdToContentBase64 =
(token: string) => async (fileId: string): Promise<string> => {
Expand Down Expand Up @@ -158,7 +158,7 @@ const sharedOwnPhone = (
) => (user_id === ownId) ? phone_number : undefined;

const contactToFullName = ({ first_name, last_name }: grammy.Contact) =>
first_name + (last_name ? " " + last_name : "");
first_name + (last_name ? ` ${last_name}` : "");

export const getBestPhoneFromContactShared = ({
phone_number,
Expand Down Expand Up @@ -210,7 +210,9 @@ export const makeTelegramHandler = (
letIn(
{
id: message.from.id,
tgm: new Telegraf(telegramToken, { handlerTimeout: Number.POSITIVE_INFINITY })
tgm: new Telegraf(telegramToken, {
handlerTimeout: Number.POSITIVE_INFINITY,
})
.telegram,
},
({ id, tgm }) =>
Expand Down
10 changes: 6 additions & 4 deletions src/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ type Manager = {
buffered: Record<string, WeboscketMessage[]>;
};

const makeKey = () => crypto.randomUUID();
export const makeKey = () => crypto.randomUUID();

const inject = <T extends TaskHandler>(
export const now = () => Date.now();

export const websocketInject = <T extends TaskHandler>(
send: (x: WeboscketMessage) => Promise<void>,
userId: string,
) =>
Expand Down Expand Up @@ -61,7 +63,7 @@ const inject = <T extends TaskHandler>(
const jsonOnSocket = <T>(msg: T) => (socket: WebSocket) =>
new Promise<void>((resolve) =>
socket.send(
JSON.stringify({ timestamp: Date.now(), ...msg }),
JSON.stringify({ timestamp: now(), ...msg }),
() => resolve(),
)
);
Expand Down Expand Up @@ -129,7 +131,7 @@ export const setupWebsocketOnServer = (
const { uniqueId, humanReadableId } = loginResult;
addSocket(ws, uniqueId);
if (!text) return;
inject(sendToUser(uniqueId), humanReadableId)(doTask)({ text });
websocketInject(sendToUser(uniqueId), humanReadableId)(doTask)({ text });
});
ws.on("close", () => {
removeSocket(ws);
Expand Down

0 comments on commit a47d7c9

Please sign in to comment.