docker를 활용하여 telegram에 chatgpt bot 생성하기

안녕하세요. 요즘 핫한 chatgpt를 telegram bot을 통해 사용하는 방법을 소개할까 합니다.

먼저, 텔레그램 봇을 생성해야 합니다

1.  텔레그램에서 BotFather 검색

BotFather는 텔레그램에서 공식적으로 제공하는 봇입니다. BotFather를 통해 봇을 생성하고 토큰을 발급받을 수 있습니다.

2.  봇 생성하기

BotFather 채팅방에서 "/newbot" 명령어를 입력합니다. 그리고 나서 봇의 이름과 아이디를 입력하면 됩니다. 봇 이름은 표시되는 이름이며, 아이디는 다른 사용자가 봇을 호출할 때 사용하는 이름입니다.

3.  봇 토큰 발급받기

봇을 생성한 후 BotFather가 발급해준 토큰을 복사합니다. 이 토큰은 봇과 텔레그램 API 서버 간의 통신을 위해 사용됩니다. 이 토큰을 안전한 곳에 보관하세요.
ex) Use this token to access the HTTP API:
<<이곳에 토큰 나열됨>>

토큰이 발급 되었으면, 도커가 설치된 환경에서 아래의 명령어를 실행합니다.

mkdir ~/config
touch ~/config/local.json

local.json file에 아래의 내용을 적어 넣은 후, 적절하게 수정해줘야 합니다.

{
  "debug": 1, // debug level: 0 - no debug, 1 - debug, 2 - verbose debug
  "bot": {
    "token": "TELEGRAM_BOT_TOKEN",
    "groupIds": [], // allowed group ids, leave empty to allow all
    "userIds": [], // allowed user ids, leave empty to allow all
    "chatCmd": "/chat"
  },
  "api": {
    "type": "official", // "browser", "official", "unofficial": the type of the chatgpt api to use
    "browser": {
      // Please refer to "https://github.com/transitive-bullshit/chatgpt-api/blob/v3/docs/classes/ChatGPTAPIBrowser.md#parameters"
      "email": "ACCOUNT_EMAIL",
      "password": "ACCOUNT_PASSWORD",
      "isGoogleLogin": false,
      "isProAccount": false,
      "executablePath": "",
      "nopechaKey": "",
      "captchaToken": "",
      "userDataDir": "",
      "timeoutMs": 120000 // set to 0 to disable
    },
    "official": {
      // Please refer to "https://github.com/transitive-bullshit/chatgpt-api/blob/main/docs/classes/ChatGPTAPI.md#parameters"
      "apiKey": "API_KEY",
      "apiBaseUrl": "",
      "completionParams": {},
      "systemMessage": "",
      "maxModelTokens": 0, // set to 0 to use default
      "maxResponseTokens": 0, // set to 0 to use default
      "timeoutMs": 60000 // set to 0 to disable
    },
    "unofficial": {
      // Please refer to "https://github.com/transitive-bullshit/chatgpt-api#usage---chatgptunofficialproxyapi"
      "accessToken": "ACCESS_TOKEN",
      "apiReverseProxyUrl": "",
      "model": "",
      "timeoutMs": 120000 // set to 0 to disable
    }
  },
  "proxy": "" // You can also specify the proxy using the environment variable "HTTP_PROXY"
}

3개의 방법으로 사용이 가능한데, 이 글에서는 unofficial의 방법으로 진행하겠습니다.

먼저, type을 수정해줍니다.

"type": "unofficial", // "browser", "official", "unofficial": the type of the chatgpt api to use

그리고, unofficial 부분에 내용을 채워줍니다.

"unofficial": {
      // Please refer to "https://github.com/transitive-bullshit/chatgpt-api#usage---chatgptunofficialproxyapi"
      "accessToken": "ACCESS_TOKEN",
      "apiReverseProxyUrl": "",
      "model": "gpt-3.5-turbo-0301",
      "timeoutMs": 120000 // set to 0 to disable
    }

accessToken : chatgpt에 로그인해서 얻어야 합니다. (이메일 + 암호 방식의 로그인)
https://chat.openai.com/chat에 로그인 후, https://chat.openai.com/api/auth/session 에 접속하여 accesstoken 부분 복사하여 ACCESS_TOKEN 부분에 붙여넣기

apiReverseProxyUrl : 현재 두가지 URL을 사용중입니다. 둘 중에 한가지 선택하여 붙여넣습니다.
https://bypass.duti.tech/api/conversation
https://gpt.pawan.krd/backend-api/conversation

model : 모델은 gpt-3.5-turbo-0301 을 사용 했습니다.

위와 같이 수정 후 저장하고 아래의 명령어를 수행합니다.

docker run -d -v ~/config:/app/config raineggplant/chatgpt-telegram-bot:latest

이후 bot father를 통해 생성한 봇에서 /help를 쳐서 테스트 해보고, 질의에 성공하면 완료된 것입니다.

참고)
https://www.npmjs.com/package/chatgpt?activeTab=readme
https://github.com/RainEggplant/chatgpt-telegram-bot