mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2026-01-24 23:35:44 +01:00
explicitly specify allowed chat ids
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
OPENAI_EMAIL="<YOUR_OPENAI_EMAIL>"
|
||||
OPENAI_PASSWORD="<YOUR_OPENAI_PASSWORD>"
|
||||
TELEGRAM_BOT_TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>"
|
||||
TELEGRAM_BOT_TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>"
|
||||
ALLOWED_TELEGRAM_CHAT_IDS="<CHAT_ID_1>,<CHAT_ID_2>,..."
|
||||
2
Pipfile
2
Pipfile
@@ -6,7 +6,7 @@ name = "pypi"
|
||||
[packages]
|
||||
requests = "*"
|
||||
python-telegram-bot = "==20.0a6"
|
||||
revchatgpt = "==0.0.27"
|
||||
revchatgpt = "==0.0.29"
|
||||
python-dotenv = "*"
|
||||
|
||||
[dev-packages]
|
||||
|
||||
8
Pipfile.lock
generated
8
Pipfile.lock
generated
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "557c367f69ef8765a9e83a3e70059fd7a703dfd16238ce989fb4c993181a14b1"
|
||||
"sha256": "75d8a7b37f5ed40cfacc81af9b6de25dbc5207c9a8da362d7afeeb89e0c52ffa"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
@@ -188,11 +188,11 @@
|
||||
},
|
||||
"revchatgpt": {
|
||||
"hashes": [
|
||||
"sha256:3e9316aa4a00c2080699bc37448836ce97cee89253fc45e6c42d8660cdfb204b",
|
||||
"sha256:9b19f998b4dbe833aa0da48b3d833f1e1d4ae3cde9cbf74f867c0ba9d34bcec3"
|
||||
"sha256:176dae1a8ec1c13c9c5dedd6b7f97083a7e66b24c208b24c001d4a5292603b59",
|
||||
"sha256:ce64ec4e48ab60be6946f7c9104c92a9baa635b06ff0feb02f599fa1cf986e59"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.0.27"
|
||||
"version": "==0.0.29"
|
||||
},
|
||||
"rfc3986": {
|
||||
"extras": [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# ChatGPT Telegram Bot
|
||||

|
||||

|
||||

|
||||
[](LICENSE)
|
||||
|
||||
A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI's [ChatGPT](https://openai.com/blog/chatgpt/) to provide answers. Ready to use with minimal configuration required.
|
||||
|
||||
11
main.py
11
main.py
@@ -1,6 +1,5 @@
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from revChatGPT.revChatGPT import Chatbot as ChatGPT3Bot
|
||||
@@ -20,7 +19,8 @@ def main():
|
||||
'password': os.environ['OPENAI_PASSWORD'],
|
||||
}
|
||||
telegram_config = {
|
||||
'telegram_bot_token': os.environ['TELEGRAM_BOT_TOKEN']
|
||||
'telegram_bot_token': os.environ['TELEGRAM_BOT_TOKEN'],
|
||||
'allowed_chats': os.environ['ALLOWED_TELEGRAM_CHAT_IDS'].split(',')
|
||||
}
|
||||
|
||||
gpt3_bot = ChatGPT3Bot(config=chatgpt_config)
|
||||
@@ -29,9 +29,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
while True:
|
||||
try:
|
||||
main()
|
||||
except:
|
||||
logging.error('An error occurred, restarting...')
|
||||
time.sleep(5)
|
||||
main()
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
import telegram.constants
|
||||
@@ -17,18 +16,30 @@ class ChatGPT3TelegramBot:
|
||||
|
||||
# Start the bot
|
||||
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if str(update.message.from_user.id) not in self.config['allowed_chats']:
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to start the bot')
|
||||
return
|
||||
|
||||
logging.info('Bot started')
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a Chat-GPT3 Bot, please talk to me!")
|
||||
|
||||
# Reset the conversation
|
||||
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if str(update.message.from_user.id) not in self.config['allowed_chats']:
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to reset the bot')
|
||||
return
|
||||
|
||||
logging.info('Resetting the conversation...')
|
||||
self.gpt3_bot.reset_chat()
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text="Done!")
|
||||
|
||||
# React to messages
|
||||
async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
logging.info('New message received')
|
||||
if str(update.message.from_user.id) not in self.config['allowed_chats']:
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to use the bot')
|
||||
return
|
||||
|
||||
logging.info(f'New message received from user {update.message.from_user.name}')
|
||||
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action=telegram.constants.ChatAction.TYPING)
|
||||
response = self.get_chatgpt_response(update.message.text)
|
||||
await context.bot.send_message(
|
||||
@@ -38,16 +49,12 @@ class ChatGPT3TelegramBot:
|
||||
parse_mode=telegram.constants.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
def get_chatgpt_response(self, message, retry=False) -> dict:
|
||||
def get_chatgpt_response(self, message) -> dict:
|
||||
try:
|
||||
response = self.gpt3_bot.get_chat_response(message)
|
||||
return response
|
||||
except:
|
||||
if not retry:
|
||||
self.gpt3_bot.refresh_session()
|
||||
return self.get_chatgpt_response(message, retry=True)
|
||||
else:
|
||||
return {"message": "I'm having some trouble talking to you, please try again later."}
|
||||
return {"message": "I'm having some trouble talking to you, please try again later."}
|
||||
|
||||
def run(self):
|
||||
application = ApplicationBuilder().token(self.config['telegram_bot_token']).build()
|
||||
|
||||
Reference in New Issue
Block a user