-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeachUsEnglishBot.py
More file actions
76 lines (57 loc) · 2.57 KB
/
TeachUsEnglishBot.py
File metadata and controls
76 lines (57 loc) · 2.57 KB
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
75
import sqlite3
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
ApplicationBuilder, CommandHandler, CallbackQueryHandler, ContextTypes
)
import random
from pytz import timezone
# مسیر دیتابیس SQLite
DB_PATH = 'database.db'
# نمایش کلمه تصادفی
async def send_random_word(update: Update, context: ContextTypes.DEFAULT_TYPE):
# اتصال به دیتابیس
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# انتخاب یک کلمه تصادفی
cursor.execute("SELECT English_Word, Part_of_Speech, English_Definition FROM words ORDER BY RANDOM() LIMIT 1")
row = cursor.fetchone()
conn.close()
# اگر کلمهای پیدا نشد
if not row:
await update.message.reply_text("هیچ کلمهای در دیتابیس پیدا نشد.")
return
# ساخت پیام برای ارسال
english_word, pos, definition = row
message = f"**کلمه:** {english_word}\n**نوع کلمه:** {pos}\n**تعریف:** {definition}"
keyboard = [
[InlineKeyboardButton("👍 یاد گرفتم!", callback_data='learned')],
[InlineKeyboardButton("❌ حذفش کن", callback_data='remove')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
# ارسال پیام
await update.message.reply_text(message, reply_markup=reply_markup, parse_mode="Markdown")
# مدیریت دکمهها
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
if query.data == 'learned':
await query.edit_message_text("عالی! این کلمه را یاد گرفتی. 🎉")
elif query.data == 'remove':
await query.edit_message_text("باشه، این کلمه حذف شد.")
# شروع بات
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("سلام! من TeacherBot هستم. هر روز کلمات جدیدی بهت یاد میدم.\n"
"برای شروع دستور /word رو بزن!")
def main():
# توکن بات
TOKEN = "7910930212:AAHy7BdeiKZYhPWafVVoxfkZNXbzHui9CSU"
# راهاندازی بات
app = ApplicationBuilder().token(TOKEN).build()
# دستورهای بات
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("word", send_random_word))
app.add_handler(CallbackQueryHandler(button_handler))
# اجرای بات
app.run_polling()
if __name__ == '__main__':
main()