Added version 4
This commit is contained in:
344
jokes_bot/v3.0/README.md
Normal file
344
jokes_bot/v3.0/README.md
Normal file
@@ -0,0 +1,344 @@
|
||||
Step-by-Step Complete Setup Guide
|
||||
📋 Before We Start - Check Your Python Installation
|
||||
1. Open PowerShell:
|
||||
Press Windows + R
|
||||
|
||||
Type powershell
|
||||
|
||||
Press Enter
|
||||
|
||||
2. Check Python Version:
|
||||
powershell
|
||||
python --version
|
||||
# Should show: Python 3.x.x
|
||||
|
||||
# If that doesn't work, try:
|
||||
python3 --version
|
||||
# Or on some systems:
|
||||
py --version
|
||||
📁 Create Your Project Folder
|
||||
3. Navigate to Documents and Create Folder:
|
||||
powershell
|
||||
# Go to Documents folder
|
||||
cd Documents
|
||||
|
||||
# Create a new folder using format: [firstname]_[surname initial]
|
||||
# Example if your name is Ivan Lim:
|
||||
mkdir ivan_l
|
||||
|
||||
# Go into your new folder
|
||||
cd ivan_l
|
||||
|
||||
# Verify location
|
||||
pwd
|
||||
🎯 Get the Template Repository
|
||||
4. Access the Template Repository:
|
||||
Go to: https://gitea.techshare.cc/technolyceum/ai6-m3
|
||||
|
||||
Find the template named: [firstname]_[surname initial]_ai6-m3
|
||||
|
||||
Click on the template
|
||||
|
||||
Click the "Clone" button and copy the URL
|
||||
|
||||
5. Clone the Repository:
|
||||
powershell
|
||||
# Clone using the copied URL
|
||||
git clone https://gitea.techshare.cc/technolyceum/ai6-m3/[firstname]_[surname initial]_ai6-m3.git
|
||||
|
||||
# Go into the cloned folder
|
||||
cd [firstname]_[surname initial]_ai6-m3
|
||||
🐍 Set Up Virtual Environment
|
||||
6. Create and Activate Virtual Environment:
|
||||
powershell
|
||||
# Create virtual environment
|
||||
python -m venv venv
|
||||
|
||||
# Activate it
|
||||
.\venv\Scripts\Activate.ps1
|
||||
|
||||
# If you see a security warning:
|
||||
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
.\venv\Scripts\Activate.ps1
|
||||
✅ Success: You should see (venv) at the start of your command line!
|
||||
|
||||
📝 Type Your Bot Code
|
||||
7. Open Python IDLE:
|
||||
powershell
|
||||
# Open IDLE from PowerShell
|
||||
python -m idlelib
|
||||
8. Create app.py in IDLE:
|
||||
In IDLE: Click File → New File
|
||||
|
||||
TYPE the following code line by line:
|
||||
|
||||
python
|
||||
from telegram import Update
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
||||
import sqlite3
|
||||
|
||||
TOKEN = "TEACHER_WILL_PROVIDE_THIS"
|
||||
|
||||
waiting = {}
|
||||
|
||||
async def start(update, context):
|
||||
await update.message.reply_text("🤣 Joke Bot!\n/joke - get joke\n/addjoke - add 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:")
|
||||
|
||||
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")
|
||||
|
||||
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()
|
||||
Click File → Save As
|
||||
|
||||
Navigate to your project folder
|
||||
|
||||
Save as: app.py
|
||||
|
||||
Click Save
|
||||
|
||||
📦 Install Dependencies
|
||||
9. Install Required Package:
|
||||
powershell
|
||||
# Make sure venv is activated
|
||||
pip install python-telegram-bot
|
||||
|
||||
# Check installation
|
||||
pip show python-telegram-bot
|
||||
10. Create requirements.txt:
|
||||
powershell
|
||||
pip freeze > requirements.txt
|
||||
Get-Content requirements.txt
|
||||
🗄️ Explore SQLite Database Using GUI
|
||||
11. First, Create the Database File:
|
||||
powershell
|
||||
# Run your bot once to create the database
|
||||
python app.py
|
||||
|
||||
# You'll see an error about the token - that's OK
|
||||
# Press Ctrl+C to stop it after 2 seconds
|
||||
12. Install DB Browser for SQLite:
|
||||
Go to: https://sqlitebrowser.org/dl/
|
||||
|
||||
Download the standard installer for Windows
|
||||
|
||||
Run the installer
|
||||
|
||||
Follow installation steps (just click Next)
|
||||
|
||||
Keep all default options
|
||||
|
||||
13. Open Your Database in DB Browser:
|
||||
Open File Explorer (Windows key + E)
|
||||
|
||||
Navigate to your project folder:
|
||||
|
||||
text
|
||||
C:\Users\YourName\Documents\[firstname]_[surname initial]\[firstname]_[surname initial]_ai6-m3
|
||||
Find jokes.db file
|
||||
|
||||
Double-click jokes.db
|
||||
|
||||
It should open in DB Browser for SQLite
|
||||
|
||||
If it doesn't, open DB Browser first, then click "Open Database"
|
||||
|
||||
14. Explore Database in DB Browser:
|
||||
Tab 1: Database Structure
|
||||
See the jokes table
|
||||
|
||||
See the 3 columns: text, user, name
|
||||
|
||||
Check data types: TEXT, TEXT, TEXT
|
||||
|
||||
Tab 2: Browse Data
|
||||
Click "Browse Data" tab
|
||||
|
||||
Select table: jokes
|
||||
|
||||
See any jokes already in the table
|
||||
|
||||
Table will be empty at first
|
||||
|
||||
15. Add Jokes Using DB Browser:
|
||||
Click "Browse Data" tab
|
||||
|
||||
Click New Record button (or press Insert key)
|
||||
|
||||
Fill in the fields:
|
||||
|
||||
text: Your joke (e.g., "Why did the chicken cross the road?")
|
||||
|
||||
user: Your user ID number (e.g., 12345)
|
||||
|
||||
name: Your name (e.g., "Ivan")
|
||||
|
||||
Click Apply button to save
|
||||
|
||||
Add 2-3 jokes this way
|
||||
|
||||
16. Edit Jokes Using DB Browser:
|
||||
Click on a joke in the table
|
||||
|
||||
Click inside any field
|
||||
|
||||
Change the text
|
||||
|
||||
Click Apply to save changes
|
||||
|
||||
17. Delete Jokes Using DB Browser:
|
||||
Click on a joke row
|
||||
|
||||
Click Delete Record button (or press Delete key)
|
||||
|
||||
Click Apply to confirm
|
||||
|
||||
18. Run SQL Queries in DB Browser:
|
||||
Click "Execute SQL" tab
|
||||
|
||||
Type SQL commands:
|
||||
|
||||
sql
|
||||
-- See all jokes
|
||||
SELECT * FROM jokes;
|
||||
|
||||
-- Count jokes
|
||||
SELECT COUNT(*) FROM jokes;
|
||||
|
||||
-- See specific user's jokes
|
||||
SELECT * FROM jokes WHERE name = 'Ivan';
|
||||
|
||||
-- Add a joke via SQL
|
||||
INSERT INTO jokes VALUES ('What do you call a bear with no teeth? A gummy bear!', 999, 'SQLUser');
|
||||
Click Execute (play button) to run
|
||||
|
||||
Click Write Changes to save
|
||||
|
||||
🔍 Quick Database Exploration Tasks
|
||||
Task 1: View Empty Database
|
||||
Open DB Browser
|
||||
|
||||
Open jokes.db
|
||||
|
||||
Check "Browse Data" tab
|
||||
|
||||
Table should be empty
|
||||
|
||||
Task 2: Add Jokes via GUI
|
||||
Click "New Record"
|
||||
|
||||
Add 3 different jokes
|
||||
|
||||
Use different names for each
|
||||
|
||||
Save all with "Apply"
|
||||
|
||||
Task 3: View What You Added
|
||||
Stay in "Browse Data" tab
|
||||
|
||||
Scroll to see all jokes
|
||||
|
||||
Notice each row has a rowid (automatic number)
|
||||
|
||||
Task 4: Edit a Joke
|
||||
Click on any joke
|
||||
|
||||
Change the text
|
||||
|
||||
Click "Apply"
|
||||
|
||||
Task 5: Delete a Joke
|
||||
Click on a joke row
|
||||
|
||||
Click "Delete Record"
|
||||
|
||||
Click "Apply"
|
||||
|
||||
Task 6: Use SQL Tab
|
||||
Go to "Execute SQL" tab
|
||||
|
||||
Type: SELECT COUNT(*) FROM jokes;
|
||||
|
||||
Click "Execute"
|
||||
|
||||
See result in bottom pane
|
||||
|
||||
🎯 What You Should See in DB Browser
|
||||
Database Structure:
|
||||
Tables: 1 table (jokes)
|
||||
|
||||
Columns: text, user, name (all TEXT type)
|
||||
|
||||
Row count: Shows at bottom
|
||||
|
||||
After Adding Jokes:
|
||||
Each row shows in table
|
||||
|
||||
Can sort by clicking column headers
|
||||
|
||||
Can filter using "Filter" box
|
||||
|
||||
File Size Growth:
|
||||
Each joke adds to file size
|
||||
|
||||
File updates instantly when you click "Apply"
|
||||
|
||||
💡 Tips for DB Browser
|
||||
Always click "Apply" after changes
|
||||
|
||||
"Write Changes" saves to disk
|
||||
|
||||
"Revert Changes" undoes unsaved changes
|
||||
|
||||
Use filter to find specific jokes
|
||||
|
||||
Export data if you want to backup
|
||||
|
||||
🔧 If Something Goes Wrong
|
||||
Database won't open:
|
||||
powershell
|
||||
# Delete and recreate
|
||||
Remove-Item jokes.db -ErrorAction SilentlyContinue
|
||||
|
||||
# Run bot to create fresh
|
||||
python app.py
|
||||
# Press Ctrl+C after error
|
||||
|
||||
# Now open in DB Browser
|
||||
DB Browser not installed:
|
||||
Go to https://sqlitebrowser.org/dl/
|
||||
|
||||
Download and install
|
||||
|
||||
Or use online SQLite viewer
|
||||
|
||||
Remember: DB Browser is the easiest way to see your database. No coding needed!
|
||||
|
||||
Note: Teacher will provide the Telegram bot token later. For now, practice adding and viewing jokes in DB Browser.
|
||||
Reference in New Issue
Block a user