File size: 2,056 Bytes
bb6e1c1 | 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 73 74 | import os
TELEGRAM_TEMPLATE = """"""
import os
import logging
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
TOKEN = os.getenv("TELEGRAM_TOKEN")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hello! I'm your assistant bot.")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(update.message.text)
def main():
if not TOKEN:
raise RuntimeError("Set TELEGRAM_TOKEN env var")
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
app.run_polling()
if __name__ == "__main__":
main()
""""""
DISCORD_TEMPLATE = """"""
import os
import discord
TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
@client.event
async def on_message(message: discord.Message):
if message.author == client.user:
return
if message.content.startswith("!ping"):
await message.channel.send("pong")
def main():
if not TOKEN:
raise RuntimeError("Set DISCORD_TOKEN env var")
client.run(TOKEN)
if __name__ == "__main__":
main()
""""""
def create_bot(bot_type: str, output_dir: str = "bots") -> str:
os.makedirs(output_dir, exist_ok=True)
if bot_type == "telegram":
path = os.path.join(output_dir, "telegram_bot.py")
with open(path, "w", encoding="utf-8") as f:
f.write(TELEGRAM_TEMPLATE)
return path
elif bot_type == "discord":
path = os.path.join(output_dir, "discord_bot.py")
with open(path, "w", encoding="utf-8") as f:
f.write(DISCORD_TEMPLATE)
return path
else:
raise ValueError("Unsupported bot_type. Use 'telegram' or 'discord'.") |