Telegramtechbot / app.py
Bloodlyghoul's picture
Rename telegram_bot.py to app.py
6d161fb verified
Raw
History Blame Contribute Delete
4.24 kB
from telegram import Update, InputFile
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import os
# Reusing functions from Gradio logic
def generate_code(description: str):
templates = {
"hello world": "print('Hello, World!')",
"file reader": """
with open('example.txt', 'r') as file:
content = file.read()
print(content)
""",
"basic calculator": """
def calculator(a, b, operation):
if operation == 'add':
return a + b
elif operation == 'subtract':
return a - b
elif operation == 'multiply':
return a * b
elif operation == 'divide' and b != 0:
return a / b
else:
return 'Invalid operation or division by zero'
print(calculator(10, 5, 'add'))
"""
}
return templates.get(description.lower(), "Sorry, I don't have a template for that yet!")
def convert_file(file_path: str):
base, ext = os.path.splitext(file_path)
if ext == ".txt":
new_file = base + ".csv"
with open(file_path, "r") as f:
content = f.readlines()
with open(new_file, "w") as f:
for line in content:
f.write(",".join(line.split()) + "\n")
return new_file
return None
def diagnose_system(issue: str):
suggestions = {
"slow pc": "Try clearing temporary files, disabling startup programs, and checking for malware.",
"internet issues": "Restart your router, check DNS settings, or contact your ISP.",
"high memory usage": "Check running processes in Task Manager and close unnecessary programs."
}
return suggestions.get(issue.lower(), "Sorry, I don't have a suggestion for that issue.")
# Telegram bot commands
def start(update: Update, context: CallbackContext):
update.message.reply_text(
"Welcome to Tech Guru Bot! Here’s what I can do:\n"
"- /generate_code <description>: Generate a Python code snippet.\n"
"- /diagnose <issue>: Provide tech troubleshooting tips.\n"
"- /convert_file: Upload a .txt file, and I’ll convert it to .csv.\n"
"Try any command!"
)
def generate_code_command(update: Update, context: CallbackContext):
if len(context.args) == 0:
update.message.reply_text("Usage: /generate_code <description>")
return
description = " ".join(context.args)
code = generate_code(description)
update.message.reply_text(f"Generated Code:\n\n{code}")
def diagnose_command(update: Update, context: CallbackContext):
if len(context.args) == 0:
update.message.reply_text("Usage: /diagnose <issue>")
return
issue = " ".join(context.args)
suggestion = diagnose_system(issue)
update.message.reply_text(suggestion)
def convert_file_command(update: Update, context: CallbackContext):
update.message.reply_text("Please upload the .txt file you want to convert.")
def handle_file_upload(update: Update, context: CallbackContext):
file = update.message.document
if not file:
update.message.reply_text("No file uploaded. Please try again.")
return
file_path = file.file_id + file.file_name
file.download(file_path)
converted_file = convert_file(file_path)
if converted_file:
with open(converted_file, "rb") as f:
update.message.reply_document(InputFile(f))
os.remove(converted_file)
else:
update.message.reply_text("Unsupported file format. Only .txt files are supported.")
os.remove(file_path)
# Main function to start the bot
def main():
BOT_TOKEN = "7684829859:AAHABWrxsMDF3L9Xo4A5xZBwtIu76dL19dY" # Replace with your bot token from BotFather
updater = Updater(BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("generate_code", generate_code_command))
dispatcher.add_handler(CommandHandler("diagnose", diagnose_command))
dispatcher.add_handler(CommandHandler("convert_file", convert_file_command))
dispatcher.add_handler(MessageHandler(Filters.document, handle_file_upload))
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()