Added sql
This commit is contained in:
12
jokes-bot-v3.0/database.py
Normal file
12
jokes-bot-v3.0/database.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import sqlite3
|
||||
conn = sqlite3.connect('jokes.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS jokes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
joke TEXT NOT NULL,
|
||||
contributor TEXT NOT NULL,
|
||||
published TEXT NOT NULL
|
||||
)''')
|
||||
conn.commit()
|
||||
print("✅ Database and table created successfully!")
|
||||
conn.close()
|
||||
@@ -1,75 +0,0 @@
|
||||
# Debug version with logging
|
||||
from telegram import Update
|
||||
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||
from database import JokeDatabase
|
||||
import os
|
||||
import logging
|
||||
|
||||
# Enable detailed logging
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
level=logging.DEBUG # Changed to DEBUG
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
BOT_TOKEN = "7864875699:AAEWf6ff1DYNzPvW6Dbn7D2W5aavg9KPhgY"
|
||||
db = JokeDatabase()
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
logger.info(f"📱 /start from user: {update.effective_user.username}")
|
||||
await update.message.reply_text("🤖 Hi! I'm your Joke Bot!\nCommands: /joke, /addjoke")
|
||||
|
||||
async def get_random_joke(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
logger.info(f"🎲 /joke from user: {update.effective_user.username}")
|
||||
joke = db.get_random_joke()
|
||||
|
||||
if not joke:
|
||||
await update.message.reply_text("No jokes yet! Use /addjoke to add one.")
|
||||
return
|
||||
|
||||
await update.message.reply_text(joke['joke_text'])
|
||||
|
||||
async def add_new_joke(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
logger.info(f"➕ /addjoke from user: {update.effective_user.username}")
|
||||
logger.info(f" Args: {context.args}")
|
||||
|
||||
if not context.args:
|
||||
await update.message.reply_text("Usage: /addjoke [your joke]")
|
||||
return
|
||||
|
||||
joke_text = " ".join(context.args)
|
||||
user = update.effective_user
|
||||
|
||||
logger.info(f" User ID: {user.id}, Username: {user.username}")
|
||||
logger.info(f" Joke text (first 50 chars): {joke_text[:50]}...")
|
||||
|
||||
# Add to database
|
||||
joke_id = db.add_joke(joke_text, user.id, user.username or user.first_name)
|
||||
logger.info(f" Database returned joke_id: {joke_id}")
|
||||
|
||||
await update.message.reply_text("✅ Joke added to database!")
|
||||
|
||||
def main():
|
||||
print("🔍 DEBUG MODE - Starting bot with detailed logs...")
|
||||
print(f"📊 Database: {os.path.abspath('jokes.db')}")
|
||||
|
||||
app = ApplicationBuilder()\
|
||||
.token(BOT_TOKEN)\
|
||||
.read_timeout(30)\
|
||||
.write_timeout(30)\
|
||||
.connect_timeout(30)\
|
||||
.pool_timeout(30)\
|
||||
.build()
|
||||
|
||||
app.add_handler(CommandHandler("start", start))
|
||||
app.add_handler(CommandHandler("joke", get_random_joke))
|
||||
app.add_handler(CommandHandler("addjoke", add_new_joke))
|
||||
|
||||
print("✅ Debug bot is running! Check logs below...")
|
||||
print("📱 Test in Telegram with /addjoke test joke")
|
||||
print("🛑 Press Ctrl+C to stop")
|
||||
|
||||
app.run_polling()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Binary file not shown.
BIN
jokes-bot-v3.0/jokes.db-journal
Normal file
BIN
jokes-bot-v3.0/jokes.db-journal
Normal file
Binary file not shown.
33
jokes-bot-v3.0/jokes_v3.py
Normal file
33
jokes-bot-v3.0/jokes_v3.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# simple_joke_menu.py
|
||||
import sqlite3
|
||||
import random
|
||||
|
||||
db = sqlite3.connect('jokes.db')
|
||||
|
||||
while True:
|
||||
print("\n1. Get joke")
|
||||
print("2. Add joke")
|
||||
print("3. Quit")
|
||||
|
||||
choice = input("Your choice: ")
|
||||
|
||||
if choice == "1":
|
||||
joke = db.execute("SELECT joke FROM jokes ORDER BY RANDOM() LIMIT 1").fetchone()
|
||||
if joke:
|
||||
print(f"\n🤣 {joke[0]}")
|
||||
else:
|
||||
print("No jokes yet!")
|
||||
|
||||
elif choice == "2":
|
||||
new = input("Your joke: ")
|
||||
if new:
|
||||
name = input("Your name: ") or "Friend"
|
||||
db.execute("INSERT INTO jokes (joke, contributor) VALUES (?, ?)", (new, name))
|
||||
db.commit()
|
||||
print("Saved!")
|
||||
|
||||
elif choice == "3":
|
||||
break
|
||||
|
||||
db.close()
|
||||
print("Bye!")
|
||||
42
jokes-bot-v3.0/sample_jokes.sql
Normal file
42
jokes-bot-v3.0/sample_jokes.sql
Normal file
@@ -0,0 +1,42 @@
|
||||
-- jokes_database_fixed.sql
|
||||
-- Create and populate jokes table with column name 'joke'
|
||||
|
||||
-- Step 1: Create the jokes table
|
||||
CREATE TABLE IF NOT EXISTS jokes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
joke TEXT NOT NULL, -- CHANGED FROM 'joke_text' TO 'joke'
|
||||
contributor TEXT,
|
||||
published DATE DEFAULT CURRENT_DATE
|
||||
);
|
||||
|
||||
-- Step 2: Insert 25 sample jokes
|
||||
INSERT INTO jokes (joke, contributor, published) VALUES -- CHANGED COLUMN NAME HERE
|
||||
('Why don''t scientists trust atoms? Because they make up everything!', 'Science Fan', '2024-01-15'),
|
||||
('Why did the scarecrow win an award? He was outstanding in his field!', 'Farm Humor', '2024-01-16'),
|
||||
('What do you call a fake noodle? An impasta!', 'Italian Chef', '2024-01-17'),
|
||||
('Why did the bicycle fall over? Because it was two-tired!', 'Bike Enthusiast', '2024-01-18'),
|
||||
('What do you call a bear with no teeth? A gummy bear!', 'Animal Lover', '2024-01-19'),
|
||||
('Why don''t eggs tell jokes? They''d crack each other up!', 'Breakfast Club', '2024-01-20'),
|
||||
('What do you call cheese that isn''t yours? Nacho cheese!', 'Cheese Master', '2024-01-21'),
|
||||
('Why did the math book look so sad? Because it had too many problems!', 'Math Teacher', '2024-01-22'),
|
||||
('What do you get when you cross a snowman with a vampire? Frostbite!', 'Winter Joker', '2024-01-23'),
|
||||
('Why don''t skeletons fight each other? They don''t have the guts!', 'Bone Collector', '2024-01-24'),
|
||||
('Why did the Python programmer quit? He didn''t get arrays!', 'Python Dev', '2024-01-25'),
|
||||
('What do computers eat for a snack? Microchips!', 'Tech Guru', '2024-01-26'),
|
||||
('Why do programmers prefer dark mode? Because light attracts bugs!', 'Code Master', '2024-01-27'),
|
||||
('How many programmers does it take to change a light bulb? None, that''s a hardware issue!', 'IT Department', '2024-01-28'),
|
||||
('Why did the database administrator leave his wife? She had one-to-many relationships!', 'SQL Expert', '2024-01-29'),
|
||||
('What''s a programmer''s favorite hangout place? Foo Bar!', 'Coder Club', '2024-01-30'),
|
||||
('Why don''t web developers go outside? They prefer the indoors network!', 'Web Dev', '2024-01-31'),
|
||||
('What do you call a programmer from Finland? Nerdic!', 'International Joker', '2024-02-01'),
|
||||
('Why did the developer go broke? Because he used up all his cache!', 'Finance Guy', '2024-02-02'),
|
||||
('What''s the object-oriented way to become wealthy? Inheritance!', 'OOP Fan', '2024-02-03'),
|
||||
('Why do Java developers wear glasses? Because they don''t C#!', 'Language Wars', '2024-02-04'),
|
||||
('What''s a programmer''s favorite animal? The Python!', 'Snake Charmer', '2024-02-05'),
|
||||
('Why did the computer go to the doctor? Because it had a virus!', 'IT Support', '2024-02-06'),
|
||||
('What do you call a computer that sings? A Dell!', 'Musical Tech', '2024-02-07'),
|
||||
('Why did the smartphone go to school? To improve its connection!', 'Mobile Guru', '2024-02-08');
|
||||
|
||||
-- Step 3: Verify the data was inserted
|
||||
SELECT '✅ Database created successfully!' as message;
|
||||
SELECT '📊 Total jokes inserted: ' || COUNT(*) as joke_count FROM jokes;
|
||||
@@ -1,44 +0,0 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from telegram import Update
|
||||
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
BOT_TOKEN = "7864875699:AAEWf6ff1DYNzPvW6Dbn7D2W5aavg9KPhgY"
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text("✅ Simple test works!")
|
||||
|
||||
async def main():
|
||||
print("🧪 Testing Telegram connection...")
|
||||
try:
|
||||
app = ApplicationBuilder()\
|
||||
.token(BOT_TOKEN)\
|
||||
.read_timeout(30)\
|
||||
.write_timeout(30)\
|
||||
.connect_timeout(30)\
|
||||
.pool_timeout(30)\
|
||||
.build()
|
||||
|
||||
app.add_handler(CommandHandler("start", start))
|
||||
|
||||
print("🤖 Starting bot...")
|
||||
await app.initialize()
|
||||
await app.start()
|
||||
print("✅ Bot started! Type /start in Telegram to test.")
|
||||
print("⏳ Running for 15 seconds...")
|
||||
|
||||
# Run for 15 seconds
|
||||
await asyncio.sleep(15)
|
||||
|
||||
await app.stop()
|
||||
await app.shutdown()
|
||||
print("✅ Test complete - no network errors!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
print("\n💡 Try: pip install python-telegram-bot==13.15")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user