Skip to content

Commit

Permalink
Merge pull request #33 from RainEggplant/refactor-docker
Browse files Browse the repository at this point in the history
Automatically build and push to Docker Hub (fix #26)
  • Loading branch information
RainEggplant authored Mar 7, 2023
2 parents 77102c3 + f74ff94 commit e1fe47d
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 53 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Publish Docker image

on:
release:
types: [published]
workflow_dispatch:

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Browserless
- name: Extract metadata for Docker (browserless)
id: meta
uses: docker/metadata-action@v4
with:
images: RainEggplant/chatgpt-telegram-bot
tags: |
type=semver,pattern={{version}}
- name: Build and push (browserless)
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# Browser-based
- name: Extract metadata for Docker (browser-based)
id: meta_browser
uses: docker/metadata-action@v4
with:
images: RainEggplant/chatgpt-telegram-bot
flavor: |
suffix=-browser,onlatest=true
tags: |
type=semver,pattern={{version}}
- name: Build and push (browser-based)
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile.browser
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta_browser.outputs.tags }}
labels: ${{ steps.meta_browser.outputs.labels }}
23 changes: 9 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
FROM node:18.12.1 as builder
# Builder stage
FROM node:lts-alpine AS builder

RUN npm install -g pnpm

WORKDIR /app

COPY package.json pnpm-lock.yaml ./

RUN pnpm install
RUN pnpm install --frozen-lockfile --ignore-scripts

COPY . .

RUN pnpm build

FROM node:18.12.1

RUN wget -qO - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg && \
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" | tee /etc/apt/sources.list.d/google-chrome.list && \
apt-get update && \
apt-get install -y xvfb google-chrome-stable

WORKDIR /app
# Runner stage
FROM node:lts-alpine

RUN npm install -g pnpm

COPY package.json pnpm-lock.yaml ./
WORKDIR /app

RUN pnpm install --prod --ignore-scripts
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --ignore-scripts --prod --no-optional

COPY --from=builder /app/dist ./dist

CMD xvfb-run -a --server-args="-screen 0 1280x800x24 -ac -nolisten tcp -dpi 96 +extension RANDR" pnpm start
CMD pnpm start
29 changes: 29 additions & 0 deletions Dockerfile.browser
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Builder stage
FROM node:lts-alpine AS builder

RUN npm install -g pnpm

WORKDIR /app

COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --ignore-scripts

COPY . .
RUN pnpm build


# Runner stage
FROM node:lts

RUN apt update && apt install -y chromium xvfb
RUN npm install -g pnpm
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

WORKDIR /app

COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --ignore-scripts --prod

COPY --from=builder /app/dist ./dist

CMD xvfb-run -a --server-args="-screen 0 1280x800x24 -ac -nolisten tcp -dpi 96 +extension RANDR" pnpm start
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,15 @@ A ChatGPT bot for Telegram based on Node.js. Support both browserless and browse

### Start the server

#### Option 1: Node
To get started, follow these steps:

1. Create `local.json` under the `config/` folder. You can copy the `config/default.json` as a template.
2. Modify the `local.json` following the instructions in the file. The settings in `local.json` will override the default settings in `default.json`.
- Set `api.type` to `browser` if you want to use the browser-based API. Then provide the OpenAI / Google / Microsoft credentials and other settings. You can refer to [this](https://github.com/transitive-bullshit/chatgpt-api/tree/v3#authentication) and [this](https://github.com/transitive-bullshit/chatgpt-api/blob/v3/docs/classes/ChatGPTAPIBrowser.md#parameters) for more details. Make sure you have a Chromium-based browser installed.
- Set `api.type` to `official` if you want to use the browserless official API. Then provide your [OpenAI API Key](https://platform.openai.com/overview) and other settings. You can refer to [this](https://github.com/transitive-bullshit/chatgpt-api#usage---chatgptapi) for more details.
> **Warning**
>
> Using the browserless API may result in charges based on the model you use, as defined in the `api.official.completionParams` (the default value depends on the version of your `chatgpt` node module). Get more details about this from [the issue section](https://github.com/transitive-bullshit/chatgpt-api/issues) of the API repository.
>
> ~~Alternatively, if you prefer to avoid charges, you can utilize the community reverse proxy servers that mimic OpenAI's completions API. Please refer to [this](https://github.com/transitive-bullshit/chatgpt-api/blob/main/demos/demo-reverse-proxy.ts) and [this](https://github.com/waylaidwanderer/node-chatgpt-api#using-a-reverse-proxy) for more details.~~ (This method has been patched by OpenAI. You can use the unofficial API instead.)
1. Clone this project.
2. Create `local.json` under the `config/` folder. You can copy the `config/default.json` as a template.
3. Modify the `local.json` following the instructions in the file. The settings in `local.json` will override the default settings in `default.json`.
- Set `api.type` to `official` if you want to use the browserless official API. Then provide your [OpenAI API Key](https://platform.openai.com/overview) and other settings. You can refer to [this](https://github.com/transitive-bullshit/chatgpt-api#usage---chatgptapi) for more details. Note that this will cost your credits.
- Set `api.type` to `unofficial` if you want to use the browserless unofficial API. Then provide your OpenAI access token ([how to get your access token?](https://github.com/transitive-bullshit/chatgpt-api#access-token)) and other settings. You can refer to [this](https://github.com/transitive-bullshit/chatgpt-api#usage---chatgptunofficialproxyapi) for more details.
- Set `api.type` to `browser` if you want to use the browser-based API (not recommended). Then provide the OpenAI / Google / Microsoft credentials and other settings. You can refer to [this](https://github.com/transitive-bullshit/chatgpt-api/tree/v3#authentication) and [this](https://github.com/transitive-bullshit/chatgpt-api/blob/v3/docs/classes/ChatGPTAPIBrowser.md#parameters) for more details. Make sure you have a Chromium-based browser installed.

Then you can start the bot with:

Expand All @@ -121,6 +118,20 @@ pnpm install
pnpm build && pnpm start
```

#### Option 2: Docker

To get started, follow these steps:

1. Create a folder named `config` and create a `local.json` file in it. You can follow the instructions in the "Choice #1: Node" section to customize the settings.
2. Run the following command to start the bot:

```shell
docker run -d -v ./config:/app/config raineggplant/chatgpt-telegram-bot:latest
```

This will pull the latest image that only supports the browserless API. If you want to use the browser-based API, you can add a `-browser` suffix to the tag, e.g., `raineggplant/chatgpt-telegram-bot:latest-browser`.


### Chat with the bot in Telegram

To chat with the bot in Telegram, you can:
Expand All @@ -142,7 +153,7 @@ The bot also has several commands.
## Advanced
### Running the bot on a headless server (browser-based API)
### Running the bot on a headless server (browser-based API only)
You can use [Xvfb](https://www.x.org/releases/X11R7.6/doc/man/man1/Xvfb.1.xhtml) to create a virtual framebuffer on a headless server and run this program:
Expand All @@ -152,14 +163,6 @@ xvfb-run -a --server-args="-screen 0 1280x800x24 -nolisten tcp -dpi 96 +extensio
We recommend you to use Google auth to avoid the complicated login Recaptchas. If you use a OpenAI account, you may have to use nopecha or 2captcha or manually solve the Recaptcha (by connecting to the display server using x11vnc). For more details about CAPTCHA solving, please refer to [the api repository](https://github.com/transitive-bullshit/chatgpt-api/tree/v3#captchas).
#### Docker

You can also try this docker image by running the following command from the project root folder:

```shell
docker compose up
```

## Credits
- [ChatGPT API](https://github.com/transitive-bullshit/chatgpt-api): Node.js client for the unofficial ChatGPT API.
Expand Down
7 changes: 0 additions & 7 deletions docker-compose.yml

This file was deleted.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
},
"dependencies": {
"chatgpt": "^5.0.6",
"chatgpt-v3": "npm:[email protected]",
"config": "^3.3.9",
"dotenv": "^16.0.3",
"extensionless": "^1.1.0",
Expand All @@ -56,7 +55,10 @@
"node-fetch": "^3.3.0",
"node-telegram-bot-api": "^0.60.0",
"promise-queue": "^2.2.5",
"puppeteer": "^19.7.2",
"telegramify-markdown": "^1.0.6"
},
"optionalDependencies": {
"chatgpt-v3": "npm:[email protected]",
"puppeteer": "^19.7.2"
}
}
Loading

0 comments on commit e1fe47d

Please sign in to comment.