mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2026-01-05 22:15:54 +01:00
Allow everyone to message the bot by default
This commit is contained in:
13
README.md
13
README.md
@@ -34,17 +34,20 @@ pipenv shell
|
||||
```
|
||||
|
||||
### Configuration
|
||||
Customize the configuration by copying `.env.example` and renaming it to `.env`, then editing the settings as desired.
|
||||
Customize the configuration by copying `.env.example` and renaming it to `.env`, then editing the settings as desired:
|
||||
```
|
||||
OPENAI_EMAIL="<YOUR_OPENAI_EMAIL>"
|
||||
OPENAI_PASSWORD="<YOUR_OPENAI_PASSWORD>"
|
||||
TELEGRAM_BOT_TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>"
|
||||
```
|
||||
* `OPENAI_EMAIL,OPENAI_PASSWORD`: Your OpenAI credentials (these are only sent to the OpenAI server to periodically refresh the access token and never shared). You can read more about it [here](https://github.com/acheong08/ChatGPT)
|
||||
* `TELEGRAM_BOT_TOKEN`: Your Telegram bot's token, obtained using [BotFather](http://t.me/botfather) (see [tutorial](https://core.telegram.org/bots/tutorial#obtain-your-bot-token))
|
||||
|
||||
Additional optional (but recommended) configuration values:
|
||||
```
|
||||
ALLOWED_TELEGRAM_USER_IDS="<USER_ID_1>,<USER_ID_2>,..."
|
||||
```
|
||||
You'll need to provide:
|
||||
1. Your OpenAI credentials (these are only sent to the OpenAI server to periodically refresh the access token and never shared). You can read more about it [here](https://github.com/acheong08/ChatGPT)
|
||||
2. Your Telegram bot's token, obtained using [BotFather](http://t.me/botfather) (see [tutorial](https://core.telegram.org/bots/tutorial#obtain-your-bot-token))
|
||||
3. A comma-separated list of Telegram user IDs that are allowed to interact with the bot (use [getidsbot](https://t.me/getidsbot) to find your user ID). If you'd like to allow *everyone* instead, see [#6](https://github.com/n3d1117/chatgpt-telegram-bot/issues/6).
|
||||
* `ALLOWED_TELEGRAM_USER_IDS`: A comma-separated list of Telegram user IDs that are allowed to interact with the bot (use [getidsbot](https://t.me/getidsbot) to find your user ID). **Important**: by default, *everyone* is allowed (`*`)
|
||||
|
||||
### Run
|
||||
Use the following command to start the bot:
|
||||
|
||||
6
main.py
6
main.py
@@ -14,13 +14,17 @@ def main():
|
||||
level=logging.INFO
|
||||
)
|
||||
|
||||
if 'TELEGRAM_BOT_TOKEN' not in os.environ:
|
||||
logging.error('Telegram bot token not found in environment variables')
|
||||
exit(1)
|
||||
|
||||
chatgpt_config = {
|
||||
'email': os.environ['OPENAI_EMAIL'],
|
||||
'password': os.environ['OPENAI_PASSWORD'],
|
||||
}
|
||||
telegram_config = {
|
||||
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
||||
'allowed_user_ids': os.environ['ALLOWED_TELEGRAM_USER_IDS'].split(',')
|
||||
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*')
|
||||
}
|
||||
|
||||
gpt3_bot = ChatGPT3Bot(config=chatgpt_config, debug=True)
|
||||
|
||||
@@ -83,7 +83,7 @@ class ChatGPT3TelegramBot:
|
||||
response = await self.gpt3_bot.get_chat_response(message)
|
||||
return response
|
||||
except Exception as e:
|
||||
logging.info(f'Error while getting the response: {e}')
|
||||
logging.info(f'Error while getting the response: {str(e)}')
|
||||
return {"message": "I'm having some trouble talking to you, please try again later."}
|
||||
|
||||
async def send_disallowed_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
@@ -106,7 +106,9 @@ class ChatGPT3TelegramBot:
|
||||
"""
|
||||
Checks if the user is allowed to use the bot.
|
||||
"""
|
||||
return str(update.message.from_user.id) in self.config['allowed_user_ids']
|
||||
if self.config['allowed_user_ids'] == '*':
|
||||
return True
|
||||
return str(update.message.from_user.id) in self.config['allowed_user_ids'].split(',')
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user