Skip to content

Commit

Permalink
Merge branch 'feature/voice-chat-mode'
Browse files Browse the repository at this point in the history
  • Loading branch information
n3d1117 committed Mar 6, 2023
2 parents 06c7972 + 8fab052 commit 02dfd99
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,USER_ID_2"
SHOW_USAGE=false

# Max number of messages to keep in memory, after which the conversation will be summarised
MAX_HISTORY_SIZE=10
MAX_HISTORY_SIZE=10

# Whether to answer to voice messages with the transcript or with a ChatGPT response of the transcript
VOICE_REPLY_WITH_TRANSCRIPT_ONLY=true
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,USER_ID_2,..." # Defaults to "*" (everyone)
PROXY="YOUR_PROXY" # e.g. "http://localhost:8080", defaults to none
SHOW_USAGE=true # Defaults to false
MAX_HISTORY_SIZE=15 # Defaults to 10
VOICE_REPLY_WITH_TRANSCRIPT_ONLY=false # Defaults to true
```
* `OPENAI_API_KEY`: Your OpenAI API key, you can get it 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))
* `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). **Note**: 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
* `MAX_HISTORY_SIZE`: Max number of messages to keep in memory, after which the conversation will be summarised to avoid excessive token usage ([#34](https://github.com/n3d1117/chatgpt-telegram-bot/issues/34))
* `VOICE_REPLY_WITH_TRANSCRIPT_ONLY`: Whether to answer to voice messages with the transcript only or with a ChatGPT response of the transcript ([#38](https://github.com/n3d1117/chatgpt-telegram-bot/issues/38))

Additional model parameters can be configured from the `main.py` file:
```python
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def main():
telegram_config = {
'token': os.environ['TELEGRAM_BOT_TOKEN'],
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*'),
'proxy': os.environ.get('PROXY', None)
'proxy': os.environ.get('PROXY', None),
'voice_reply_transcript': os.environ.get('VOICE_REPLY_WITH_TRANSCRIPT_ONLY', 'true').lower() == 'true',
}

# Setup and run ChatGPT and Telegram bot
Expand Down
24 changes: 17 additions & 7 deletions telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,23 @@ async def transcribe(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
# Transcribe the audio file
transcript = self.openai.transcribe(filename_mp3)

# Send the transcript
await context.bot.send_message(
chat_id=chat_id,
reply_to_message_id=update.message.message_id,
text=transcript,
parse_mode=constants.ParseMode.MARKDOWN
)
if self.config['voice_reply_transcript']:
# Send the transcript
await context.bot.send_message(
chat_id=chat_id,
reply_to_message_id=update.message.message_id,
text=f'_Transcript:_\n"{transcript}"',
parse_mode=constants.ParseMode.MARKDOWN
)
else:
# Send the response of the transcript
response = self.openai.get_chat_response(chat_id=chat_id, query=transcript)
await context.bot.send_message(
chat_id=chat_id,
reply_to_message_id=update.message.message_id,
text=response,
parse_mode=constants.ParseMode.MARKDOWN
)
except Exception as e:
logging.exception(e)
await context.bot.send_message(
Expand Down

0 comments on commit 02dfd99

Please sign in to comment.