Skip to content

Commit

Permalink
Merge pull request #2 from xmtp-labs/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
humanagent authored Jun 3, 2024
2 parents 418c343 + 77d521d commit f50e8d4
Show file tree
Hide file tree
Showing 53 changed files with 452 additions and 497 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ To learn more about the contents of this repository, see this README and the REA
- [`playground`](/packages/playground): A visual playground for creating groups
- [`docs`](/packages/docs): Documentation for botkit

### Examples
### Templates

- [`conversational`](/examples/conversational): A simple, customizable, and powerful conversational framework
- [`gm`](/examples/gm): A simple bot that responds `gm` to any incoming messages
- [`subscription`](/examples/subscription): A simple, customizable, and powerful conversational framework
- [`gpt`](/examples/gpt): A bot that relays incoming messages to OpenAI's ChatGPT
- [`group`](/examples/group): Group bot example.

Expand Down
3 changes: 0 additions & 3 deletions examples/conversational/.env.example

This file was deleted.

2 changes: 0 additions & 2 deletions examples/gm/.env.example

This file was deleted.

21 changes: 0 additions & 21 deletions examples/gm/LICENSE.md

This file was deleted.

40 changes: 0 additions & 40 deletions examples/gm/README.md

This file was deleted.

32 changes: 0 additions & 32 deletions examples/gm/package.json

This file was deleted.

10 changes: 0 additions & 10 deletions examples/gm/src/index.ts

This file was deleted.

7 changes: 0 additions & 7 deletions examples/gm/tsconfig.json

This file was deleted.

9 changes: 4 additions & 5 deletions examples/gpt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ let conversationHistory = [
];

run(async (context: HandlerContext) => {
const { message } = context;

const { content } = message;
const { content, contentType, senderAddress } = context.message;
const { typeId } = contentType;

try {
const { reply, messages } = await openaiCall(
content,
conversationHistory,
message.senderAddress,
senderAddress,
systemPrompt,
);
conversationHistory = messages; // Update the conversation history
await context.reply(reply);
await context.reply(reply as string);
} catch (error) {
// Handle the error, for example, by sending an error message to the user
await context.reply(
Expand Down
2 changes: 1 addition & 1 deletion examples/group/src/bots/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function handler(context: HandlerContext) {
slot: "https://slot-machine-frame.vercel.app/",
guess: "https://farguessr.vercel.app/",
};

console.log(extracted);
switch (extracted.command) {
case "framedl":
case "slot":
Expand Down
2 changes: 1 addition & 1 deletion examples/group/src/bots/gpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function handler(context: HandlerContext, commands: any) {
This group bot has many commands avaiable: ${JSON.stringify(commands)}\n
When possible, answer with the command from the list for the user to perform. put this command in a new line\n
The message was sent by ${senderAddress}`;
console.log(systemPrompt);

let message = content.replace("@bot", "");
let { reply } = await openaiCall(message, senderAddress, systemPrompt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const commands = [
command: "/baseframe mint [collection_address] [token_id]",
description: "Create (mint) a new token or NFT.",
},
{ command: "/baseframe help", description: "Display this help message." },
{
command: "/baseframe help",
description: "Display this help message.",
},
{ command: "/baseframe show", description: "Show the whole frame." },
],
},
Expand Down
18 changes: 9 additions & 9 deletions examples/group/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "dotenv/config";
import { run, HandlerContext, xmtpClient } from "@xmtp/botkit";
import { users } from "./lib/users.js";
import { handler as baseHandler } from "./bots/baseframe.js";
import { commands } from "./lib/commands.js";
import { commands } from "./commands.js";
import { handler as basebetHandler } from "./bots/basebet.js";
import { handler as degenHandler } from "./bots/degen.js";
import { handler as gptHandler } from "./bots/gpt.js";
Expand All @@ -16,21 +16,23 @@ const newBotConfig = {
},
};

// Main bot runner
run(async (context: HandlerContext) => {
const { content, contentType, senderAddress } = context.message;
const { typeId } = contentType;

populateFakeUsers(context);

console.log("message", content, contentType, senderAddress, typeId);
if (typeId == "reaction") {
const { action, content: emoji } = content;
if (emoji == "degen" && action == "added") await degenHandler(context);
} else if (typeId == "reply") {
const { receiver, content: reply } = content;
if (receiver && reply.includes("degen")) await degenHandler(context);
} else if (typeId == "silent") {
if (1 == 1) context.grant_access(content);
} else if (content == "/access" && typeId == "silent") {
if (senderAddress) {
/*here put the token gated logic*/
context.grant_access();
}
} else if (typeId == "text") {
if (content.startsWith("@bot")) {
await gptHandler(context, commands);
Expand All @@ -46,14 +48,12 @@ run(async (context: HandlerContext) => {
await degenHandler(context);
} else if (content.startsWith("/games")) {
await gamesHandler(context);
} else if (content.startsWith("/ping")) {
await context.reply("");
} else if (content.startsWith("/help")) {
const intro =
"Available experiences:\n" +
commands
.flatMap((bot) => bot.commands)
.map((command) => `${command.command} - ${command.description}`)
.flatMap((bot: any) => bot.commands)
.map((command: any) => `${command.command} - ${command.description}`)
.join("\n") +
"\nUse these commands to interact with specific bots.";
context.reply(intro);
Expand Down
3 changes: 3 additions & 0 deletions examples/subscription/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
KEY= # 0x... the private key of the bot (with the 0x prefix)
XMTP_ENV=production # or `dev`
REDIS_CONNECTION_STRING=redis://default:[email protected]:39045
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"format": "turbo run format",
"format:check": "turbo run format:check",
"publish": "yarn build && changeset publish",
"start:conversational": "turbo run start --filter='./examples/conversational'",
"start:docs": "turbo run dev --filter='./packages/docs'",
"start:gm": "turbo run start --filter='./examples/gm'",
"start:gpt": "turbo run start --filter='./examples/gpt'",
"start:group": "turbo run start --filter='./examples/group'",
"start:playground": "turbo run dev --filter='./packages/playground'",
"start:subscription": "turbo run start --filter='./examples/subscription'",
"typecheck": "FORCE_COLOR=1 turbo run typecheck"
},
"dependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/botkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ run(async (context: HandlerContext) => {

## Examples

- [Gm](/examples/gm): Get started with a simple gm bot.
- [Conversational](/examples/conversational): Drive retention with conversations and subscriptions with redisDb.
- [Subscription](/examples/subscription): Drive retention with conversations and subscriptions with redisDb.
- [GPT](/examples/gpt): Relay messages through Open AI APIs.
- [Group](/examples/group): Group bot example.

Expand Down
15 changes: 5 additions & 10 deletions packages/botkit/src/content-types/Silent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ export class SilentCodec {
}

encode(silent) {
const { sender, reference, content, metadata } = silent;
const { content, metadata } = silent;

return {
type: ContentTypeSilent,
parameters: {},
content: new TextEncoder().encode(
JSON.stringify({ sender, reference, content, metadata }),
),
content: new TextEncoder().encode(JSON.stringify({ content, metadata })),
};
}

Expand All @@ -30,16 +28,13 @@ export class SilentCodec {

try {
const silent = JSON.parse(decodedContent);
const { sender, reference, content, metadata } = silent;
return { sender, reference, content, metadata };
const { content, metadata } = silent;
return { content, metadata };
} catch (e) {
const parameters = encodedContent.parameters;
return {
action: parameters.action,
reference: parameters.reference,
schema: parameters.schema,
content: decodedContent,
receiver: parameters.receiver,
metadata: parameters.metadata,
};
}
}
Expand Down
39 changes: 26 additions & 13 deletions packages/botkit/src/lib/handler-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,41 @@ export default class HandlerContext {
async textReply(content: any) {
await this.message.conversation.send(content);
}
async grant_access(content: any) {
let acceptAccess = {
content: "grant_access",
sender: content.sender,
reference: content.reference,
metadata: { hello: "world" },
};
await this.message.conversation.send(acceptAccess, {
contentType: ContentTypeSilent,
});
async grant_access() {
await this.message.conversation.send(
{
content: "grant_access",
metadata: {
access: true,
...this.context,
},
},
{
contentType: ContentTypeSilent,
},
);
}
async ping() {
await this.message.conversation.send(
{
content: "ping",
metadata: { ...this.context },
},
{
contentType: ContentTypeSilent,
},
);
}
async reply(
message: string,
receivers: string[] = [],
messageId: string = "",
) {
let context = JSON.stringify(this.context);
const botMessage = {
sender: this.message.senderAddress,
receivers: receivers, // Assuming receiver is available in context
receivers: receivers,
content: message,
context,
...this.context,
reference: messageId,
};

Expand Down
16 changes: 12 additions & 4 deletions packages/botkit/src/lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export default async function run(handler: Handler, newBotConfig?: any) {
//if a bot speaks do nothing
continue;
}

const context = new HandlerContext(message, newBotConfig, client.address);

await handler(context);
const context = new HandlerContext(
message,
newBotConfig?.context ?? {},
client.address,
);
if (
message.contentType.typeId == "silent" &&
message.content?.content === "/ping"
) {
//if a bot speaks do nothing
context.ping();
} else await handler(context);
} catch (e) {
console.log(`error`, e);
}
Expand Down
Loading

0 comments on commit f50e8d4

Please sign in to comment.