Stas-GPT is an AI bot designed to provide responses to user messages using OpenAI API. The bot is implemented as a Cloudflare Worker, which handles incoming VKontakte (VK) message events and uses the OpenAI API to generate responses.
- Install Node.js (version 14.x or newer) on your local machine.
- Install Wrangler CLI (version 2.13.0 or newer) by running
npm install -g wrangler
. - Clone this repository to your local machine:
git clone https://github.com/st4s1k/stas-gpt.git
. - Change the current directory to the project folder:
cd stas-gpt
. - Install the required dependencies by running
npm install
.
To connect the Stas-GPT bot with a VK community, follow these steps:
- Create a new VK community or use an existing one.
- Go to the community's "Manage" section.
- In the left-hand menu, select "API usage" and then click on "Create token."
- Assign the necessary permissions for the bot, such as "messages" and "users." Save the token for later use in the
.dev.vars
file. - In the left-hand menu, select "Callback API" under the "API usage" section.
- Set the API version to 5.131 or the version specified in the
wrangler.toml
file. - Add a server and enter the Cloudflare Worker`s URL as the server URL.
- Save the server and copy the "Confirmation string" provided by VK.
- In the
wrangler.toml
file, set theVK_CONFIRMATION_CODE
value to the copied "Confirmation string." - Set up event types to handle by selecting "message_new" and any other required event types.
- Save the settings in VK and proceed with the installation and setup of the Stas-GPT bot as described below.
- Create a new KV namespace in your Cloudflare account.
wrangler kv:namespace create "STAS_GPT_KV" wrangler kv:namespace create "STAS_GPT_KV" --preview
- Take note of the "Namespace ID" and "Preview Namespace ID" values.
- Update the
wrangler.toml
file by replacing the values ofid
andpreview_id
for the[[kv_namespaces]]
binding with the respective "Namespace ID" and "Preview Namespace ID" values you obtained in the previous step.
-
Create a new file named
.dev.vars
in the root directory of the project. This file will store your API tokens and should not be included in version control. -
In the
.dev.vars
file, add your OpenAI API token and VK community API token in the following format:OPENAI_API_TOKEN=your_openai_api_token VK_COMMUNITY_API_TOKEN=your_vk_community_api_token
-
Save the
.dev.vars
file and close it.
- Deploy the bot to Cloudflare Workers using
npm run deploy
.
- Start the local development server using
npm run start
. This allows you to test and debug the worker locally. - Deploy the project to Cloudflare using
npm run deploy
. - To build the project without deploying, use
npm run build
. The build artifacts will be generated in thedist
folder. - Tail the worker logs in real-time using
npm run tail
.
- node-fetch (v3.3.1): A light-weight module that brings window.fetch to Node.js
- @cloudflare/workers-types (v4.20230321.0): TypeScript declarations for Cloudflare Workers
- @typescript-eslint/eslint-plugin (v5.57.0): TypeScript plugin with TSLint rules for ESLint
- @typescript-eslint/parser (v5.57.0): TypeScript parser for ESLint
- eslint (v8.37.0): JavaScript and TypeScript linter
- npm (v9.6.2): Node package manager
- rollup-plugin-node-polyfills (v0.2.1): Rollup plugin for Node.js built-in modules
- typescript (v5.0.3): TypeScript language compiler
- wrangler (v2.13.0): Command-line tool for managing and deploying Cloudflare Workers
This project is released under the MIT License. See the LICENSE file for details.
(README.md (and most of the code) generated with the help of GPT-4)