Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update v0.0.24 #89

Merged
merged 12 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,23 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Copy files
run: yarn copy
- name: Create release pull request
if: github.event_name == 'push'
uses: changesets/action@v1
with:
title: "release: version packages"
commit: "release: version packages"
publish: yarn publish
setupGitUser: true
createGithubReleases: true
bump: "patch"
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
continue-on-error: true
- name: Publish to npm
if: github.event_name == 'push'
run: yarn publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

## - name: Create release pull request
## if: github.event_name == 'push'
## uses: changesets/action@v1
## with:
## title: "release: version packages"
## commit: "release: version packages"
## publish: yarn publish
## setupGitUser: true
## createGithubReleases: true
## bump: "patch"
## env:
## GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
## NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
## continue-on-error: true
## - name: Publish to npm
## if: github.event_name == 'push'
## run: yarn publish --access public
## env:
## NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"changeset": "yarn copy && changeset add --type patch",
"clean": "turbo run clean && rm -rf node_modules && rm -rf .turbo && yarn cache clean",
"copy": "cd packages/create-message-kit && yarn copy && cd .. && cd ..",
"dev": "cd packages/message-kit && yarn build:watch && bun link",
"dev:docs": "cd packages/docs && yarn dev",
"dev:gm": "cd examples/gm && yarn dev",
"dev:group": "cd examples/group && yarn dev",
"dev:one-to-one": "cd examples/one-to-one && yarn dev",
"format": "turbo run format",
"format:check": "turbo run format:check",
"publish": "turbo run build --filter='./packages/*' --filter='!./packages/docs' && changeset publish",
"publish": "turbo run build --filter='./packages/*' --filter='!./packages/docs' && yarn copy && changeset publish",
"test": "FORCE_COLOR=1 turbo run test --force",
"typecheck": "FORCE_COLOR=1 turbo run typecheck"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/docs/pages/deployment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ REDIS_CONNECTION_STRING= # the connection string for the Redis database
4. **Environment Variables**: Set up environment variables in Railway.

![](/img/railway/5.gif)

5. **Cache**: The cache is stored in the `.cache` folder where the conversation history is stored. If deleted or restarted, the bot will not have access to previous messages.

![](/img/railway/volume.png)

- Railway allows to attach a volume to the container to preserve the cache between deployments
6 changes: 6 additions & 0 deletions packages/docs/pages/directory.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ These are some open source examples of mini-apps built with MessageKit.
- **How to use**: [Send a DM](https://converse.xyz/dm/wordlebot.eth) to `wordlebot.eth` in Converse.
- **Source Code**: [Bot](https:/ephemerahq/wordle-bot)

#### 💱 Swapbot

- **Description**: Swap tokens through messaging.
- **How to use**: [Send a DM](https://converse.xyz/dm/swapbot.converse.xyz) to `swapbot.converse.xyz` in Converse.
- **Source Code**: [Bot](https:/fabriguespe/swap-bot)

### 🎫 Invite Bot

- **Description**: Invite bot for Events linked to a Notion db.
Expand Down
1 change: 1 addition & 0 deletions packages/docs/pages/use-cases/group/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ const commandHandlers: CommandHandlers = {
"- 💧 faucetbot.eth : Delivers Faucet funds to devs on Testnet\n\n\n" +
"- 🛍️ thegeneralstore.eth : Simple ecommerce storefront for hackathon goods\n\n\n" +
"- 📅 wordlebot.eth : Play daily to the WORDLE game through messaging.\n\n\n" +
"- 💱 swapbot.converse.xyz : Swap tokens through messaging.\n\n\n" +
"- 🪨 urltomint.eth : Turn a Zora url into a mint frame.\n\n\n" +
"To learn how to build your own app, visit MessageKit: https://message-kit.vercel.app/\n\n" +
"To publish your app, visit Directory: https://message-kit.vercel.app/directory\n\n" +
Expand Down
Binary file added packages/docs/public/img/railway/volume.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/message-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xmtp/message-kit",
"version": "0.0.19",
"version": "0.0.24-beta.0",
"license": "MIT",
"type": "module",
"exports": {
Expand Down
2 changes: 2 additions & 0 deletions packages/message-kit/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ const external = [
"ethers",
"@xmtp/proto",
"@xmtp/grpc-api-client",
"path",
"viem",
"viem/accounts",
"fs/promises",
"fs",
"viem/chains",
"dotenv/config",
Expand Down
1 change: 1 addition & 0 deletions packages/message-kit/src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type MessageAbstracted = {
id: string;
sent: Date;
content: any;
version: string;
sender: {
inboxId: string;
username: string;
Expand Down
97 changes: 69 additions & 28 deletions packages/message-kit/src/lib/handlerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
Client as ClientV2,
Conversation as ConversationV2,
} from "@xmtp/xmtp-js";
import fs from "fs/promises";

import type { Reaction } from "@xmtp/content-type-reaction";
import { populateUsernames } from "../helpers/usernames.js";
import { ContentTypeText } from "@xmtp/content-type-text";
Expand Down Expand Up @@ -35,7 +37,6 @@ export default class HandlerContext {
members?: User[];
commandHandlers?: CommandHandlers;
getMessageById!: (id: string) => DecodedMessage | null;

private constructor(
conversation: Conversation | ConversationV2,
{ client, v2client }: { client: Client; v2client: ClientV2 },
Expand Down Expand Up @@ -123,18 +124,9 @@ export default class HandlerContext {
sender: sender,
typeId: message.contentType.typeId,
sent: sentAt,
version: version as string,
};

if (process?.env?.MSG_LOG) {
//trim spaces from text
let content =
typeof message?.content === "string"
? message?.content
: message?.contentType.typeId;

console.log("content", content, senderAddress);
}

return context;
} else {
context.message = {
Expand All @@ -148,34 +140,71 @@ export default class HandlerContext {
},
typeId: "new_" + (context.isGroup ? "group" : "conversation"),
sent: conversation.createdAt,
version: version as string,
};
}

return context;
}

async getReplyChain(reference: string): Promise<{
messageChain: string;
receiverFromChain: string;
async getV2MessageById(reference: string): Promise<DecodedMessageV2 | null> {
const conversations = await this.v2client.conversations.list();
for (const conversation of conversations) {
const messages = await conversation.messages();
if (messages.find((m) => m.id === reference)) {
return messages.find((m) => m.id === reference) as DecodedMessageV2;
}
}
return null;
}

async getReplyChain(
reference: string,
version: "v2" | "v3",
botAddress?: string,
): Promise<{
chain: Array<{ address: string; content: string }>;
isSenderInChain: boolean;
}> {
const msg = await this.getMessageById(reference);
let receiver = this.members?.find(
(member) => member.inboxId === msg?.senderInboxId,
let msg: DecodedMessage | DecodedMessageV2 | null = null;
let senderAddress: string = "";

if (version === "v3") msg = await this.getMessageById(reference);
else if (version === "v2") msg = await this.getV2MessageById(reference);

if (!msg) {
return {
chain: [],
isSenderInChain: false,
};
}

let sender = this.members?.find(
(member) =>
member.inboxId === (msg as DecodedMessage).senderInboxId ||
member.address === (msg as DecodedMessageV2).senderAddress,
);
if (!msg) return { messageChain: "", receiverFromChain: "" };
let chain = `${msg?.content?.content ?? msg?.content}\n\n`;
senderAddress = sender?.address ?? "";

let content = msg?.content?.content ?? msg?.content;
let isSenderBot = senderAddress.toLowerCase() === botAddress?.toLowerCase();
let chain = [{ address: senderAddress, content: content }];
if (msg?.content?.reference) {
const { messageChain, receiverFromChain } = await this.getReplyChain(
msg?.content?.reference,
);
receiver = this.members?.find(
(member) => member.address === receiverFromChain,
const { chain: replyChain, isSenderInChain } = await this.getReplyChain(
msg.content.reference,
version,
botAddress,
);
chain = `${messageChain}\nUser:${chain}`;
chain = replyChain;
isSenderBot = isSenderBot || isSenderInChain;

chain.push({
address: senderAddress,
content: content,
});
}
return {
messageChain: chain,
receiverFromChain: receiver?.address ?? "",
chain: chain,
isSenderInChain: isSenderBot,
};
}
async reply(message: string) {
Expand Down Expand Up @@ -228,6 +257,18 @@ export default class HandlerContext {
}
}

async getCacheCreationDate() {
//Gets the creation date of the cache folder
//Could be used to check if the cache is outdated
//Generally indicates the deployment date of the bot
try {
const stats = await fs.stat(".cache");
const cacheCreationDate = new Date(stats.birthtime);
return cacheCreationDate;
} catch (err) {
console.error(err);
}
}
async sendTo(message: string, receivers: string[]) {
const conversations = await this.v2client.conversations.list();
//Sends a 1 to 1 to multiple users
Expand Down
35 changes: 13 additions & 22 deletions packages/message-kit/src/lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ export default async function run(handler: Handler, config?: Config) {
return;
}

if (process?.env?.MSG_LOG === "true") {
console.log(
`msg_${version}:`,
typeof message?.content === "string"
? message?.content.substring(0, 20) +
(message?.content.length > 20 ? "..." : "")
: message?.contentType?.typeId ??
message?.content?.contentType?.typeId,
);
}
const context = await HandlerContext.create(
conversation,
message,
Expand All @@ -60,15 +70,6 @@ export default async function run(handler: Handler, config?: Config) {
const stream = await client.conversations.streamAllMessages();
try {
for await (const message of stream) {
if (process?.env?.MSG_LOG) {
console.log(
`incoming_${version}:`,
typeof message?.content === "string"
? message?.content
: message?.contentType.typeId,
);
}

const conversation = await client.conversations.getConversationById(
message?.conversationId ?? "",
);
Expand All @@ -83,15 +84,6 @@ export default async function run(handler: Handler, config?: Config) {
const stream = await v2client.conversations.streamAllMessages();
try {
for await (const message of stream) {
if (process?.env?.MSG_LOG) {
console.log(
`incoming_${version}:`,
typeof message?.content === "string"
? message?.content
: message?.contentType.typeId,
);
}

handleMessage(version, message, message.conversation);
}
} catch (e) {
Expand All @@ -107,8 +99,6 @@ export default async function run(handler: Handler, config?: Config) {
const stream = await client.conversations.stream();
try {
for await (const conversation of stream) {
if (process?.env?.MSG_LOG)
console.log(`incoming_${version}`, conversation?.id);
handleConversation(version, conversation);
}
} catch (e) {
Expand All @@ -120,8 +110,6 @@ export default async function run(handler: Handler, config?: Config) {
const stream = await v2client.conversations.stream();
try {
for await (const conversation of stream) {
if (process?.env?.MSG_LOG)
console.log(`incoming_${version}`, conversation?.topic);
handleConversation(version, conversation);
}
} catch (e) {
Expand All @@ -135,6 +123,9 @@ export default async function run(handler: Handler, config?: Config) {
conversation: any,
) => {
if (conversation) {
if (process?.env?.MSG_LOG === "true")
console.log(`conv_${version}`, conversation?.id ?? conversation.topic);

try {
const context = await HandlerContext.create(
conversation,
Expand Down