mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-23 23:55:05 +01:00
@@ -38,11 +38,13 @@ Customize the configuration by copying `.env.example` and renaming it to `.env`,
|
|||||||
OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
|
OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
|
||||||
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
|
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
|
||||||
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,USER_ID_2,..." # Defaults to "*" (everyone)
|
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,USER_ID_2,..." # Defaults to "*" (everyone)
|
||||||
|
PROXY="<HTTP/HTTPS_PROXY>" # E.g. "http://localhost:8080", defaults to none
|
||||||
SHOW_USAGE=false
|
SHOW_USAGE=false
|
||||||
```
|
```
|
||||||
* `OPENAI_API_KEY`: Your OpenAI API key, get if from [here](https://platform.openai.com/account/api-keys)
|
* `OPENAI_API_KEY`: Your OpenAI API key, get if from [here](https://platform.openai.com/account/api-keys)
|
||||||
* `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))
|
* `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))
|
||||||
* `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 (`*`)
|
* `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 (`*`)
|
||||||
|
* `PROXY`: Proxy to be used for OpenAI and telegram bot
|
||||||
* `SHOW_USAGE`: Whether to show OpenAI token usage information after each response. Optional, defaults to `false`
|
* `SHOW_USAGE`: Whether to show OpenAI token usage information after each response. Optional, defaults to `false`
|
||||||
|
|
||||||
Additional optional model parameters can be configured from the `main.py` file:
|
Additional optional model parameters can be configured from the `main.py` file:
|
||||||
|
|||||||
4
main.py
4
main.py
@@ -28,6 +28,7 @@ def main():
|
|||||||
openai_config = {
|
openai_config = {
|
||||||
'api_key': os.environ['OPENAI_API_KEY'],
|
'api_key': os.environ['OPENAI_API_KEY'],
|
||||||
'show_usage': os.environ.get('SHOW_USAGE', 'false').lower() == 'true',
|
'show_usage': os.environ.get('SHOW_USAGE', 'false').lower() == 'true',
|
||||||
|
'proxy': os.environ.get('PROXY'),
|
||||||
|
|
||||||
# 'gpt-3.5-turbo' or 'gpt-3.5-turbo-0301'
|
# 'gpt-3.5-turbo' or 'gpt-3.5-turbo-0301'
|
||||||
'model': 'gpt-3.5-turbo',
|
'model': 'gpt-3.5-turbo',
|
||||||
@@ -59,7 +60,8 @@ def main():
|
|||||||
|
|
||||||
telegram_config = {
|
telegram_config = {
|
||||||
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
||||||
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*')
|
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*'),
|
||||||
|
'proxy': os.environ.get('PROXY')
|
||||||
}
|
}
|
||||||
|
|
||||||
# Setup and run ChatGPT and Telegram bot
|
# Setup and run ChatGPT and Telegram bot
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ class OpenAIHelper:
|
|||||||
:param config: A dictionary containing the GPT configuration
|
:param config: A dictionary containing the GPT configuration
|
||||||
"""
|
"""
|
||||||
openai.api_key = config['api_key']
|
openai.api_key = config['api_key']
|
||||||
|
if config.get('proxy'):
|
||||||
|
openai.proxy = config['proxy']
|
||||||
self.config = config
|
self.config = config
|
||||||
self.sessions: dict[int: list] = dict() # {chat_id: history}
|
self.sessions: dict[int: list] = dict() # {chat_id: history}
|
||||||
|
|
||||||
@@ -110,4 +112,4 @@ class OpenAIHelper:
|
|||||||
:param role: The role of the message sender
|
:param role: The role of the message sender
|
||||||
:param content: The message content
|
:param content: The message content
|
||||||
"""
|
"""
|
||||||
self.sessions[chat_id].append({"role": role, "content": content})
|
self.sessions[chat_id].append({"role": role, "content": content})
|
||||||
|
|||||||
@@ -126,7 +126,14 @@ class ChatGPT3TelegramBot:
|
|||||||
"""
|
"""
|
||||||
Runs the bot indefinitely until the user presses Ctrl+C
|
Runs the bot indefinitely until the user presses Ctrl+C
|
||||||
"""
|
"""
|
||||||
application = ApplicationBuilder().token(self.config['token']).build()
|
if self.config.get('proxy'):
|
||||||
|
proxy_url = self.config['proxy']
|
||||||
|
application = ApplicationBuilder().token(
|
||||||
|
self.config['token']).proxy_url(
|
||||||
|
proxy_url).get_updates_proxy_url(proxy_url).build()
|
||||||
|
else:
|
||||||
|
application = ApplicationBuilder().token(
|
||||||
|
self.config['token']).build()
|
||||||
|
|
||||||
application.add_handler(CommandHandler('reset', self.reset))
|
application.add_handler(CommandHandler('reset', self.reset))
|
||||||
application.add_handler(CommandHandler('help', self.help))
|
application.add_handler(CommandHandler('help', self.help))
|
||||||
|
|||||||
Reference in New Issue
Block a user