-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fabriguespe/access
Add access section
- Loading branch information
Showing
20 changed files
with
312 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
KEY= # 0x... the private key of the bot (with the 0x prefix) | ||
XMTP_ENV=production # or `dev` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 XMTP (xmtp.org) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Gm Bot | ||
|
||
Simple Gm bot | ||
|
||
## Usage | ||
|
||
```jsx | ||
import "dotenv/config"; | ||
import { xmtpClient, run, HandlerContext } from "@xmtp/botkit"; | ||
|
||
run(async (context: HandlerContext) => { | ||
const { content, senderAddress } = context.message; | ||
|
||
await context.reply(`gm`); | ||
}); | ||
``` | ||
|
||
## Running the bot | ||
|
||
> ⚠️ Bot kit is not compatible with `bun`. Use `npm` or `yarn` | ||
```bash | ||
# install dependencies | ||
yarn install | ||
|
||
# running the bot | ||
yarn build | ||
yarn start | ||
|
||
# to run with hot-reload | ||
yarn build:watch | ||
yarn start:watch | ||
``` | ||
|
||
## Variables | ||
|
||
```bash | ||
KEY= # 0x... the private key of the bot (with the 0x prefix) | ||
XMTP_ENV=production # or `dev` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "gm-bot", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"build:watch": "tsc -w", | ||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist", | ||
"start": "node dist/index.js", | ||
"start:watch": "nodemon dist/index.js", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@xmtp/botkit": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.12.7", | ||
"nodemon": "^3.1.0", | ||
"prettier": "^3.2.5", | ||
"typescript": "^5.4.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import "dotenv/config"; | ||
import { run, HandlerContext } from "@xmtp/botkit"; | ||
|
||
run(async (context: HandlerContext) => { | ||
// Get the message and the address from the sender | ||
const { content, senderAddress } = context.message; | ||
|
||
// To reply, just call `reply` on the HandlerContext. | ||
await context.reply(`gm`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@xmtp/tsconfig/base.json", | ||
"include": ["src/**/*"], | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Access | ||
|
||
For declaring that the bot will handle access we need to add the access command to the commands file | ||
|
||
```tsx [src/commands.ts] | ||
export const commands = [ | ||
{ | ||
name: "General Commands", | ||
icon: "🔧", | ||
description: "Command for managing default behaviours.", | ||
commands: [ | ||
/* Declare access command*/ | ||
{ | ||
command: "/access", | ||
description: "Grant access to a user to the bot.", | ||
}, | ||
/* Other commands */ | ||
], | ||
}, | ||
]; | ||
``` | ||
|
||
### Declare commands | ||
|
||
To declare the access command in the botkit app, you need to add the following code: | ||
|
||
```tsx | ||
import { commands } from "./commands.js"; | ||
|
||
const newBotConfig = { | ||
context: { | ||
commands: commands, | ||
}, | ||
}; | ||
|
||
run(async (context: HandlerContext) => { | ||
//Your logic here | ||
}, newBotConfig); | ||
``` | ||
|
||
For allowing access to a new user botkit receives a `silent` request with this structure | ||
|
||
```tsx | ||
{ | ||
content: "/access", | ||
metadata: {} | ||
} | ||
``` | ||
|
||
### Receive an access request | ||
|
||
To accept an access request in your app, you can use the following code: | ||
|
||
```jsx | ||
if (typeId == "silent") { | ||
const { content: command } = content; | ||
// Do something with the command | ||
if (command == "/access") | ||
if (senderAddress) { | ||
/*here put the token gated logic*/ | ||
// accept the access request | ||
context.grant_access(); | ||
} | ||
} | ||
``` | ||
|
||
### Full example | ||
|
||
```jsx [src/index.ts] | ||
import "dotenv/config"; | ||
import { run, HandlerContext } from "@xmtp/botkit"; | ||
import { commands } from "./commands.js"; | ||
|
||
const newBotConfig = { | ||
context: { | ||
commands: commands, | ||
}, | ||
}; | ||
|
||
run(async (context: HandlerContext) => { | ||
const { content, contentType, senderAddress } = context.message; | ||
const { typeId } = contentType; | ||
|
||
if (typeId == "silent") { | ||
const { content: command } = content; | ||
// Do something with the command | ||
if (command == "/access") | ||
if (senderAddress) { | ||
/*here put the token gated logic*/ | ||
// accept the access request | ||
context.grant_access(); | ||
} | ||
} | ||
context.reply("gm!"); | ||
}, newBotConfig); | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Silent | ||
|
||
For special uses cases where the app needs to send commands to a bot without being displayed in the app, botkit receives a `silent` request with this structure | ||
|
||
```tsx | ||
{ | ||
content: "command", | ||
metadata: {} | ||
} | ||
``` | ||
|
||
### Receive an silent message | ||
|
||
This example shows how recieve a silent message and grant access to a user | ||
|
||
```jsx | ||
const { content, contentType, senderAddress } = context.message; | ||
const { typeId } = contentType; | ||
|
||
if (typeId == "silent") { | ||
const { content: command } = content; | ||
// Do something with the command | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.