From 4ef255cb6f509ca8484112fbaeca2b511442f8b4 Mon Sep 17 00:00:00 2001 From: fabri Date: Sun, 23 Jun 2024 13:19:59 -0300 Subject: [PATCH] update --- .../pages/use-cases/one-to-one/broadcast.mdx | 4 +- .../pages/use-cases/one-to-one/subscribe.mdx | 110 +++++++++++------- 2 files changed, 72 insertions(+), 42 deletions(-) diff --git a/packages/docs/pages/use-cases/one-to-one/broadcast.mdx b/packages/docs/pages/use-cases/one-to-one/broadcast.mdx index 964ed5ed..6a728e3a 100644 --- a/packages/docs/pages/use-cases/one-to-one/broadcast.mdx +++ b/packages/docs/pages/use-cases/one-to-one/broadcast.mdx @@ -1,6 +1,6 @@ # Broadcast -Create a cron that sends daily messages to your **redis database** subscribers. This app can run daily or according to your logic: +Create a cron that sends daily messages to your **redis database** subscribers. This app can run daily or according to your code: ## Install dependency @@ -40,7 +40,7 @@ cron.schedule( ); ``` -## Add cron in your logic +## Add cron ```jsx [src/index.ts] async function start() { diff --git a/packages/docs/pages/use-cases/one-to-one/subscribe.mdx b/packages/docs/pages/use-cases/one-to-one/subscribe.mdx index 90a95f61..df882508 100644 --- a/packages/docs/pages/use-cases/one-to-one/subscribe.mdx +++ b/packages/docs/pages/use-cases/one-to-one/subscribe.mdx @@ -41,56 +41,86 @@ export const getRedisConfig = async (redisClient: any) => { }; ``` -## Add redis in your logic +## Add Redis Import the redis config file and send it as parameter to the `run` function. ```jsx [src/index.ts] +import "dotenv/config"; +import { getRedisClient, getRedisConfig } from "./lib/redis.js"; +import cron from "node-cron"; +import { ContentTypeText } from "@xmtp/content-type-text"; +import { xmtpClient, run, HandlerContext } from "@xmtp/message-kit"; + +//Tracks conversation steps +const inMemoryCacheStep = new Map(); + +//List of words to stop or unsubscribe. +const stopWords = ["stop", "unsubscribe", "cancel", "list"]; async function start() { - const redisClient = await getRedisClient(); // [!code hl] // [!code focus] - const appConfig = await getRedisConfig(redisClient); // [!code hl] // [!code focus] - run(async (context: HandlerContext) => { - - const { content, senderAddress } = context.message; - const lowerContent = content.toLowerCase(); - - //Handles unsubscribe and resets step - if (stopWords.some((word) => lowerContent.includes(word))) { - inMemoryCacheStep.set(senderAddress, 0); - await redisClient.del(senderAddress); // [!code hl] // [!code focus] - await context.reply( - "You are now unsubscribed. You will no longer receive updates!.", - ); - } - - const cacheStep = inMemoryCacheStep.get(senderAddress) || 0; - let message = ""; - if (cacheStep === 0) { - message = "Welcome! Choose an option:\n1. Info\n2. Subscribe"; - // Move to the next step - inMemoryCacheStep.set(senderAddress, cacheStep + 1); - } else if (cacheStep === 1) { - if (content === "1") { - message = "Here is the info."; - } else if (content === "2") { - await redisClient.set(senderAddress, "subscribed"); // [!code hl] // [!code focus] - message = - "You are now subscribed. You will receive updates.\n\ntype 'stop' to unsubscribe"; - //reset the app to the initial step + const redisClient = await getRedisClient(); + + const appConfig = { + client: await getRedisConfig(redisClient), // Send it at the apptm of the run function + }; + + run( + async (context: HandlerContext) => { + const { + message: { + content: { content: text }, + typeId, + sender: { address: senderAddress }, + }, + } = context; + + if (typeId !== "text") { + /* If the input is not text do nothing */ + return; + } + + const lowerContent = text?.toLowerCase(); + + //Handles unsubscribe and resets step + if (stopWords.some((word) => lowerContent.includes(word))) { inMemoryCacheStep.set(senderAddress, 0); + await redisClient.del(senderAddress); + await context.reply( + "You are now unsubscribed. You will no longer receive updates!.", + ); + } + + const cacheStep = inMemoryCacheStep.get(senderAddress) || 0; + let message = ""; + if (cacheStep === 0) { + message = "Welcome! Choose an option:\n1. Info\n2. Subscribe"; + // Move to the next step + inMemoryCacheStep.set(senderAddress, cacheStep + 1); + } else if (cacheStep === 1) { + if (text === "1") { + message = "Here is the info."; + } else if (text === "2") { + await redisClient.set(senderAddress, "subscribed"); //test + message = + "You are now subscribed. You will receive updates.\n\ntype 'stop' to unsubscribe"; + //reset the app to the initial step + inMemoryCacheStep.set(senderAddress, 0); + } else { + message = + "Invalid option. Please choose 1 for Info or 2 to Subscribe."; + // Keep the same step to allow for re-entry + } } else { - message = "Invalid option. Please choose 1 for Info or 2 to Subscribe."; - // Keep the same step to allow for re-entry + message = "Invalid option. Please start again."; + inMemoryCacheStep.set(senderAddress, 0); } - } else { - message = "Invalid option. Please start again."; - inMemoryCacheStep.set(senderAddress, 0); - } - //Send the message - await context.reply(message); + //Send the message + await context.reply(message); + }, //@ts-ignore + appConfig, + ); - }, appConfig); } start();