diff --git a/README.md b/README.md index 810c0ff6..f70bc9f0 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,35 @@ Get a Todo list ChatGPT plugin up and running in under 5 minutes using Python. If you do not already have plugin developer access, please [join the waitlist](https://openai.com/waitlist/plugins). -## Setup -To install the required packages for this plugin, run the following command: +## Python Setup -```bash +To install the required packages for the Python version of this plugin, run the following command: + +```bach pip install -r requirements.txt ``` -To run the plugin, enter the following command: +To run the Python plugin, enter the following command: ```bash python main.py ``` +## JavaScript Setup + +To install the required packages for the JavaScript version of this plugin, run the following command: + +```bash +npm install +``` + +To run the JavaScript plugin, enter the following command: + +```bash +node main.js +``` + Once the local server is running: 1. Navigate to https://chat.openai.com. diff --git a/main.js b/main.js new file mode 100644 index 00000000..37966e3d --- /dev/null +++ b/main.js @@ -0,0 +1,68 @@ +const express = require('express'); +const cors = require('cors'); +const fs = require('fs'); + +const app = express(); +app.use(express.json()); +app.use(cors({ origin: 'https://chat.openai.com' })); + +// Keep track of todo's. Does not persist if the Node.js process is restarted. +const TODOS = {}; + +app.post('/todos/:username', (req, res) => { + const username = req.params.username; + if (!TODOS[username]) { + TODOS[username] = []; + } + TODOS[username].push(req.body.todo); + res.status(200).send('OK'); +}); + +app.get('/todos/:username', (req, res) => { + const username = req.params.username; + res.status(200).json(TODOS[username] || []); +}); + +app.delete('/todos/:username', (req, res) => { + const username = req.params.username; + const todoIdx = req.body.todo_idx; + // fail silently, it's a simple plugin + if (0 <= todoIdx && todoIdx < TODOS[username].length) { + TODOS[username].splice(todoIdx, 1); + } + res.status(200).send('OK'); +}); + +app.get('/logo.png', (req, res) => { + res.sendFile('logo.png', { root: __dirname }, (err) => { + if (err) { + res.status(500).send(err); + } + }); +}); + +app.get('/.well-known/ai-plugin.json', (req, res) => { + fs.readFile('./.well-known/ai-plugin.json', 'utf8', (err, data) => { + if (err) { + res.status(500).send(err); + } else { + res.set('Content-Type', 'text/json'); + res.status(200).send(data); + } + }); +}); + +app.get('/openapi.yaml', (req, res) => { + fs.readFile('openapi.yaml', 'utf8', (err, data) => { + if (err) { + res.status(500).send(err); + } else { + res.set('Content-Type', 'text/yaml'); + res.status(200).send(data); + } + }); +}); + +app.listen(5003, '0.0.0.0', () => { + console.log('App is running on port 5003'); +}); diff --git a/package.json b/package.json new file mode 100644 index 00000000..eb297785 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.2" + } +}