Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriguespe committed Jun 23, 2024
1 parent 99e46ed commit 4ef255c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 42 deletions.
4 changes: 2 additions & 2 deletions packages/docs/pages/use-cases/one-to-one/broadcast.mdx
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -40,7 +40,7 @@ cron.schedule(
);
```
## Add cron in your logic
## Add cron
```jsx [src/index.ts]
async function start() {
Expand Down
110 changes: 70 additions & 40 deletions packages/docs/pages/use-cases/one-to-one/subscribe.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>();

//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();
Expand Down

0 comments on commit 4ef255c

Please sign in to comment.