Last active
March 8, 2023 14:54
-
-
Save Recursing/c85d0bb53977d9c8077ebce81d290581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tempfile | |
import subprocess | |
import openai | |
from telegram import Update, InlineQueryResultArticle, InputTextMessageContent | |
from telegram.ext import ( | |
filters, | |
MessageHandler, | |
ApplicationBuilder, | |
ContextTypes, | |
InlineQueryHandler, | |
) | |
openai.api_key = "" | |
async def ask_chat_gpt(query: str) -> str: | |
completion = await openai.ChatCompletion.acreate( | |
model="gpt-3.5-turbo", messages=[{"role": "user", "content": query},], | |
) | |
return completion["choices"][0]["message"]["content"] | |
async def handle_inline(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
query = update.inline_query.query | |
if not query or not query.strip().endswith("999"): | |
answer = "End your message with 999" | |
response = "Invalid prompt" | |
else: | |
prompt = query.strip().rstrip("9") | |
answer = await ask_chat_gpt(prompt) | |
response = "Prompt: " + prompt + "\n\n" + "Answer:\n" + answer | |
results = [] | |
results.append( | |
InlineQueryResultArticle( | |
id=hash(answer), | |
title="ChatGPT answer", | |
input_message_content=InputTextMessageContent(response), | |
description=answer, | |
) | |
) | |
await context.bot.answer_inline_query( | |
update.inline_query.id, results, cache_time=99999 | |
) | |
async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
if not update.message: | |
return | |
if not update.message.text: | |
return | |
response = await ask_chat_gpt(update.message.text) | |
await context.bot.send_message( | |
chat_id=update.effective_chat.id, | |
text=response, | |
reply_to_message_id=update.message.message_id, | |
) | |
async def transcribe_audio(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
file_id = update.message.voice.file_id | |
file = await context.bot.get_file(file_id) | |
with tempfile.NamedTemporaryFile() as fp: | |
await file.download_to_memory(fp) | |
fp.flush() | |
process = subprocess.run(["ffmpeg", "-y", "-i", fp.name, "voice.wav"]) | |
if process.returncode != 0: | |
print("Error: ", process.returncode) | |
return | |
file = open("voice.wav", "rb") | |
transcription = await openai.Audio.atranscribe("whisper-1", file) | |
text = transcription["text"] | |
await context.bot.send_message( | |
update.message.chat.id, text, reply_to_message_id=update.message.message_id | |
) | |
if __name__ == "__main__": | |
application = ( | |
ApplicationBuilder() | |
.token("") | |
.build() | |
) | |
application.add_handler(InlineQueryHandler(handle_inline)) | |
application.add_handler(MessageHandler(filters.TEXT, handle_text)) | |
application.add_handler(MessageHandler(filters.VOICE, transcribe_audio)) | |
application.run_polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment