villm / main.py
tonne's picture
change to superset
459e702
raw
history blame
No virus
2.5 kB
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters, InlineQueryHandler, CallbackContext
import openai
openai.api_key = "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"
messages = [
{
"role": "system",
"content": "Welcome to Grow Bot on telegram"
}
]
TOKEN= "6058469252:AAEIpuQPvhZfanC1nCrylNCjikYKcXmHMko"
def convert_text(text):
return text.replace("_", "\\_").replace("*", "\\*").replace("[", "\\[").replace("`", "\\`").replace(".", "\\.")
async def welcome_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Welcome to Grow Bot!")
async def echo_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
input = update.message.text
messages.append(
{"role": "user", "content": input[6:]}
)
chat = openai.ChatCompletion.create(
model = "gpt-3.5-turbo", messages = messages
)
reply = chat.choices[0].message.content
print("Reply: ", reply)
await update.message.reply_markdown(f"{reply}")
# await update.message.reply_text(f"{reply}")
async def clear_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
messages = []
await update.message.reply_text("Clear context!")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
await update.message.reply_text("Help!")
async def non_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
input = update.message.text
messages.append(
{"role": "user", "content": input}
)
chat = openai.ChatCompletion.create(
model = "gpt-3.5-turbo", messages = messages
)
messages[-1]["role"] = "assistant"
reply = chat.choices[0].message.content
print(f"Input: {input}, Reply: {reply}")
await update.message.reply_markdown(f"{reply}")
def main() -> None:
application = Application.builder().token(TOKEN).build()
application.add_handler(CommandHandler("welcome", welcome_command))
application.add_handler(CommandHandler("echo", echo_command))
application.add_handler(CommandHandler("clear", clear_command))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, non_command))
application.run_polling()
if __name__=='__main__':
main()