File size: 2,504 Bytes
459e702 0dd8f73 459e702 0dd8f73 459e702 0dd8f73 459e702 0dd8f73 459e702 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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()
|