Sql
This commit is contained in:
@@ -1,35 +1,43 @@
|
||||
# app.py — Teacher-Only Telegram Joke Bot
|
||||
from telegram import Update
|
||||
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||
import random
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
||||
import sqlite3
|
||||
|
||||
# 🔑 REPLACE WITH REAL TOKEN FROM @BotFather
|
||||
BOT_TOKEN = ""
|
||||
TOKEN = "7864875699:AAEWf6ff1DYNzPvW6Dbn7D2W5aavg9KPhgY"
|
||||
|
||||
# --- COPY STUDENT JOKES HERE BEFORE CLASS ---
|
||||
JOKE_LIST = [
|
||||
"Why did the robot go to school? To recharge his brain! 🔋",
|
||||
"Knock knock!\\nWho's there?\\nBoo!\\nBoo who?\\nDon't cry! 😂",
|
||||
"Why don't eggs tell jokes? They'd crack each other up! 🥚",
|
||||
"What do you call a penguin in the desert? Lost! 🐧",
|
||||
# Add student jokes here
|
||||
]
|
||||
waiting = {} # Track who's adding jokes
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text("🤖 Hi! I'm your Joke Bot!\nType /joke for a funny joke in English! 😄")
|
||||
async def start(update, context):
|
||||
await update.message.reply_text("🤣 Joke Bot!\n/joke - get joke\n/addjoke - add joke")
|
||||
|
||||
async def send_joke(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
joke = random.choice(JOKE_LIST)
|
||||
await update.message.reply_text(joke)
|
||||
async def joke(update, context):
|
||||
conn = sqlite3.connect('jokes.db')
|
||||
conn.execute('CREATE TABLE IF NOT EXISTS jokes (text TEXT, user TEXT, name TEXT)')
|
||||
joke = conn.execute("SELECT text FROM jokes ORDER BY RANDOM() LIMIT 1").fetchone()
|
||||
conn.close()
|
||||
await update.message.reply_text(joke[0] if joke else "No jokes! /addjoke to add one")
|
||||
|
||||
async def addjoke(update, context):
|
||||
waiting[update.effective_user.id] = True
|
||||
await update.message.reply_text("📝 Type your joke:")
|
||||
|
||||
def main():
|
||||
print("🚀 Starting Joke Bot...")
|
||||
app = ApplicationBuilder().token(BOT_TOKEN).build()
|
||||
app.add_handler(CommandHandler("start", start))
|
||||
app.add_handler(CommandHandler("joke", send_joke))
|
||||
print("✅ Bot is running! Press Ctrl+C to stop.")
|
||||
app.run_polling()
|
||||
async def handle_text(update, context):
|
||||
user_id = update.effective_user.id
|
||||
if user_id in waiting:
|
||||
conn = sqlite3.connect('jokes.db')
|
||||
conn.execute('CREATE TABLE IF NOT EXISTS jokes (text TEXT, user TEXT, name TEXT)')
|
||||
conn.execute("INSERT INTO jokes VALUES (?, ?, ?)",
|
||||
(update.message.text, user_id, update.effective_user.first_name))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
del waiting[user_id]
|
||||
await update.message.reply_text("✅ Saved!")
|
||||
else:
|
||||
await update.message.reply_text("Try /start")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
app = Application.builder().token(TOKEN).build()
|
||||
app.add_handler(CommandHandler("start", start))
|
||||
app.add_handler(CommandHandler("joke", joke))
|
||||
app.add_handler(CommandHandler("addjoke", addjoke))
|
||||
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_text))
|
||||
print("Bot running!")
|
||||
app.run_polling()
|
||||
Reference in New Issue
Block a user