Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Local Bot API support #504

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ ALLOWED_TELEGRAM_USER_IDS=USER_ID_1,USER_ID_2
# TTS_PRICES=0.015,0.030
# BOT_LANGUAGE=en
# ENABLE_VISION_FOLLOW_UP_QUESTIONS="true"
# VISION_MODEL="gpt-4-vision-preview"
# VISION_MODEL="gpt-4-vision-preview"

# Local Telegram bot API mode (enables >20MB file uploads to the bot)
# Note: Before switching to local mode you must first logout the bot from the cloud API:
# https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/logOut
#
# you must also setup your own the API and file server. For addtl details check:
# https://github.com/python-telegram-bot/python-telegram-bot/wiki/Local-Bot-API-Server
# https://github.com/tdlib/telegram-bot-api
# https://github.com/tdlib/telegram-bot-api/issues/381
#TELEGRAM_BOT_API_URL=http://telegram-bot-api.lan:8081/bot
#TELEGRAM_BOT_API_BASE_FILE_URL=http://telegram-bot-api.lan:8080/
#TELEGRAM_BOT_API_LOCAL_MODE=true
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ Check out the [Budget Manual](https://github.com/n3d1117/chatgpt-telegram-bot/di
| `WHISPER_PROMPT` | To improve the accuracy of Whisper's transcription service, especially for specific names or terms, you can set up a custom message. [Speech to text - Prompting](https://platform.openai.com/docs/guides/speech-to-text/prompting) | `-` |
| `TTS_VOICE` | The Text to Speech voice to use. Allowed values: `alloy`, `echo`, `fable`, `onyx`, `nova`, or `shimmer` | `alloy` |
| `TTS_MODEL` | The Text to Speech model to use. Allowed values: `tts-1` or `tts-1-hd` | `tts-1` |

| `TELEGRAM_BOT_API_URL` | [Telegram Bot API](https://github.com/tdlib/telegram-bot-api) URL. Before switching to self hosted API you must first logout the bot from the cloud API: `https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/logOut` |`https://api.telegram.org/bot`|
| `TELEGRAM_BOT_API_BASE_FILE_URL` | Bot API file retrival URL. Set this parameter when using `TELEGRAM_BOT_API_URL`. Refer to `.env.example` and [Local Bot API](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Local-Bot-API-Server) |`https://api.telegram.org/file/bot`|
| `TELEGRAM_BOT_API_LOCAL_MODE` | Local BOT API mode. Set to `true` when running self hosted api with the `--local` flag. Enables large file upload to the bot. |`False`|
Check out the [official API reference](https://platform.openai.com/docs/api-reference/chat) for more details.

#### Functions
Expand Down
3 changes: 3 additions & 0 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def main():
'tts_prices': [float(i) for i in os.environ.get('TTS_PRICES', "0.015,0.030").split(",")],
'transcription_price': float(os.environ.get('TRANSCRIPTION_PRICE', 0.006)),
'bot_language': os.environ.get('BOT_LANGUAGE', 'en'),
'telegram_bot_api_url': os.environ.get('TELEGRAM_BOT_API_URL', 'https://api.telegram.org/bot'),
'telegram_bot_api_base_file_url': os.environ.get('TELEGRAM_BOT_API_BASE_FILE_URL', 'https://api.telegram.org/file/bot'),
'telegram_bot_api_local_mode': os.environ.get('TELEGRAM_BOT_API_LOCAL_MODE', 'false').lower() == 'true',
}

plugin_config = {
Expand Down
3 changes: 3 additions & 0 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,9 @@ def run(self):
.get_updates_proxy_url(self.config['proxy']) \
.post_init(self.post_init) \
.concurrent_updates(True) \
.base_url(self.config['telegram_bot_api_url']) \
.base_file_url(self.config['telegram_bot_api_base_file_url']) \
.local_mode(self.config['telegram_bot_api_local_mode']) \
.build()

application.add_handler(CommandHandler('reset', self.reset))
Expand Down
6 changes: 3 additions & 3 deletions translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"vision_fail":"Failed to interpret image",
"tts_no_prompt":"Please provide text! (e.g. /tts my house)",
"tts_fail":"Failed to generate speech",
"media_download_fail":["Failed to download audio file", "Make sure the file is not too large. (max 20MB)"],
"media_download_fail":["Failed to download file", "Make sure the file is not too large. (max 20MB or 2000MB in Bot API local mode)"],
"media_type_fail":"Unsupported file type",
"transcript":"Transcript",
"answer":"Answer",
Expand Down Expand Up @@ -266,7 +266,7 @@
"image_fail":"Не удалось создать изображение",
"tts_no_prompt":"Пожалуйста, подайте текст! (например, /tts мой дом)",
"tts_fail":"Не удалось создать речь",
"media_download_fail":["Не удалось загрузить аудиофайл", "Проверьте, чтобы файл не был слишком большим. (не более 20 МБ)"],
"media_download_fail":["Не удалось загрузить аудиофайл", "Проверьте, чтобы файл не был слишком большим. (не более 20 МБ, или 2000 МБ в локальном режиме Bot API)"],
"media_type_fail":"Неподдержанный тип файла",
"transcript":"Расшифровка",
"answer":"Ответ",
Expand Down Expand Up @@ -689,7 +689,7 @@
"image_fail":"Не вдалося створити зображення",
"tts_no_prompt":"Будь ласка, надайте текст! (наприклад, /tts мій будинок)",
"tts_fail":"Не вдалося створити голос",
"media_download_fail":["Не вдалося завантажити аудіофайл", "Переконайтеся, що файл не занадто великий. (максимум 20 МБ)"],
"media_download_fail":["Не вдалося завантажити аудіофайл", "Переконайтеся, що файл не занадто великий. (максимум 20 МБ, або 2000 МБ з локальним Bot API)"],
"media_type_fail":"Непідтримуваний тип файлу",
"transcript":"Транскрипт",
"answer":"Відповідь",
Expand Down