mirror of
https://github.com/father-bot/chatgpt_telegram_bot.git
synced 2026-06-13 03:54:57 +03:00
Working v0
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
FROM python:3.8-slim
|
||||
|
||||
ENV PYTHONFAULTHANDLER=1
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
ENV PYTHONHASHSEED=random
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PIP_NO_CACHE_DIR=off
|
||||
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
|
||||
ENV PIP_DEFAULT_TIMEOUT=100
|
||||
|
||||
# Env vars
|
||||
ENV TELEGRAM_TOKEN ${TELEGRAM_TOKEN}
|
||||
ENV CHATGPT_SESSION_TOKEN ${CHATGPT_SESSION_TOKEN}
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y python3 python3-pip python-dev build-essential python3-venv
|
||||
|
||||
RUN mkdir -p /chatgpt_telegram_bot
|
||||
ADD . /chatgpt_telegram_bot
|
||||
WORKDIR /chatgpt_telegram_bot
|
||||
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
CMD ["bash"]
|
||||
@@ -1 +1,24 @@
|
||||
# chatgpt_telegram_bot
|
||||
# ChatGPT Telegram Bot
|
||||
|
||||
## Setup
|
||||
1. Get your ChatGPT session token. You can find it in [chat.openai.com](https://chat.openai.com) cookies (key: `"__Secure-next-auth.session-token"`)
|
||||
|
||||
2. Get your Telegram bot token from [@BotFather](https://t.me/BotFather)
|
||||
|
||||
3. Edit `config.env.example` to add your tokens. It looks like this:
|
||||
```bash
|
||||
TELEGRAM_TOKEN="<YOU TELEGRAM BOT TOKE>"
|
||||
CHATGPT_SESSION_TOKEN="<YOUR CHATGPT SESSION ID>"
|
||||
ALLOWED_TELEGRAM_USERS="<@USERNAME>"
|
||||
```
|
||||
|
||||
4. Rename `config.env.example` to `config.env`:
|
||||
```bash
|
||||
mv config.env.example config.env
|
||||
```
|
||||
|
||||
## Run
|
||||
|
||||
```bash
|
||||
docker compose up --build
|
||||
```
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import os
|
||||
import logging
|
||||
import traceback
|
||||
import html
|
||||
import json
|
||||
|
||||
from telegram import Update
|
||||
from telegram.ext import (
|
||||
ApplicationBuilder,
|
||||
CallbackContext,
|
||||
CommandHandler,
|
||||
MessageHandler,
|
||||
filters
|
||||
)
|
||||
from telegram.constants import ParseMode
|
||||
|
||||
from revChatGPT.revChatGPT import Chatbot
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ChatGPT
|
||||
chatgpt = Chatbot({"session_token": os.environ["CHATGPT_SESSION_TOKEN"], "Authorization": ""}, conversation_id=None)
|
||||
chatgpt.reset_chat()
|
||||
chatgpt.refresh_session()
|
||||
|
||||
|
||||
async def start_handler(update: Update, context: CallbackContext):
|
||||
await update.message.reply_text("I'm ChatGPT in Telegram 🤖")
|
||||
|
||||
|
||||
async def prompt_handle(update: Update, context: CallbackContext):
|
||||
prompt = update.message.text
|
||||
r = chatgpt.get_chat_response(prompt)
|
||||
await update.message.reply_text(r["message"])
|
||||
|
||||
|
||||
async def reset_thread_handler(update: Update, context: CallbackContext):
|
||||
chatgpt.reset_chat()
|
||||
chatgpt.refresh_session()
|
||||
|
||||
await update.message.reply_text("Thread reset ✅")
|
||||
|
||||
|
||||
async def error_handler(update: Update, context: CallbackContext) -> None:
|
||||
logger.error(msg="Exception while handling an update:", exc_info=context.error)
|
||||
|
||||
# collect error message
|
||||
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
||||
tb_string = "".join(tb_list)[:2000]
|
||||
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
||||
message = (
|
||||
f"An exception was raised while handling an update\n"
|
||||
f"<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}"
|
||||
"</pre>\n\n"
|
||||
f"<pre>{html.escape(tb_string)}</pre>"
|
||||
)
|
||||
|
||||
await context.bot.send_message(update.effective_chat.id, message, parse_mode=ParseMode.HTML)
|
||||
|
||||
|
||||
def run_bot() -> None:
|
||||
application = (
|
||||
ApplicationBuilder()
|
||||
.token(os.environ["TELEGRAM_TOKEN"])
|
||||
.build()
|
||||
)
|
||||
|
||||
# add handlers
|
||||
if os.environ["ALLOWED_TELEGRAM_USERNAMES"] == "":
|
||||
user_filter = None
|
||||
else:
|
||||
allowed_usernames = list(map(str.strip, os.environ["ALLOWED_TELEGRAM_USERNAMES"].split(",")))
|
||||
user_filter = filters.User(username=allowed_usernames)
|
||||
|
||||
application.add_handler(CommandHandler('start', start_handler, filters=user_filter))
|
||||
application.add_handler(CommandHandler('reset', reset_thread_handler, filters=user_filter))
|
||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND & user_filter, prompt_handle))
|
||||
|
||||
application.add_error_handler(error_handler)
|
||||
|
||||
# start the bot
|
||||
application.run_polling()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_bot()
|
||||
@@ -0,0 +1,3 @@
|
||||
TELEGRAM_TOKEN="<YOU TELEGRAM BOT TOKE>"
|
||||
CHATGPT_SESSION_TOKEN="<YOUR CHATGPT SESSION ID>" # find it in chatgpt.openai.com cookies (key: "__Secure-next-auth.session-token")
|
||||
ALLOWED_TELEGRAM_USERNAMES="<@USERNAME>" # can be empty string "" to allow any users; to add multiple users: "@USERNAME_1,@USERNAME_1"
|
||||
@@ -0,0 +1,12 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
chatgpt_telegram_bot:
|
||||
container_name: chatgpt_telegram_bot
|
||||
command: python3 bot.py
|
||||
restart: always
|
||||
env_file:
|
||||
- config.env
|
||||
build:
|
||||
context: "."
|
||||
dockerfile: Dockerfile
|
||||
@@ -0,0 +1,2 @@
|
||||
python-telegram-bot==20.0a0
|
||||
revChatGPT==0.0.14
|
||||
Reference in New Issue
Block a user