diff --git a/README.md b/README.md index bfe6e04..2267401 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,73 @@ async function runFlow() { runFlow(); ``` +## Run a Flow with Tweet Data + +Flow inputs can include arrays and nested objects. This example loads a +[TweetClaw](https://github.com/Xquik-dev/tweetclaw) JSON export and passes the +tweet text into a flow for enrichment, routing, or review: + +```typescript +import { readFile } from "node:fs/promises"; +import { GumloopClient } from "gumloop"; + +type TweetRecord = { + text?: unknown; +}; + +function getTweetRecords(exportData: unknown): unknown { + if (Array.isArray(exportData)) { + return exportData; + } + + if (exportData && typeof exportData === "object") { + const root = exportData as Record; + if ("tweets" in root) { + return root.tweets; + } + if ("data" in root) { + return root.data; + } + if ("results" in root) { + return root.results; + } + return [root]; + } + + return []; +} + +async function loadTweetText(path: string): Promise { + const exportData = JSON.parse(await readFile(path, "utf8")) as unknown; + const records = getTweetRecords(exportData); + + if (!Array.isArray(records)) { + return []; + } + + return records.flatMap((item): string[] => { + const tweet = item as TweetRecord; + return typeof tweet.text === "string" ? [tweet.text] : []; + }); +} + +const client = new GumloopClient({ + apiKey: "your_api_key", + userId: "your_user_id", +}); + +async function runTweetFlow() { + const output = await client.runFlow("your_flow_id", { + tweets: await loadTweetText("tweetclaw-export.json"), + source: "TweetClaw", + }); + + console.log(output); +} + +runTweetFlow(); +``` + ## Project-Level Usage If you're working within a Gumloop project, you can initialize the client with a project ID: