A small trivia bot made for my yotube tutorial
If anything wasn't clear you can PM me at @painor
بوت بسيط تم صنعه من اجل فيديو اليوتيب التعليمي
اذا كان شيء غير واضح يمكنك التواصل معي في التلغرام على المعرف التالي @painor
A small trivia bot made for my yotube tutorial
If anything wasn't clear you can PM me at @painor
بوت بسيط تم صنعه من اجل فيديو اليوتيب التعليمي
اذا كان شيء غير واضح يمكنك التواصل معي في التلغرام على المعرف التالي @painor
from telethon import TelegramClient, events, Button | |
import aiohttp | |
import html | |
import random | |
client = TelegramClient("bot", , "") | |
client.start() | |
@client.on(events.NewMessage(pattern=r"^/start$")) | |
async def start(event): | |
await event.reply("Hi I am a trivia bot. To start a new question send /new") | |
trivia_url = "https://opentdb.com/api.php?amount=1" | |
@client.on(events.NewMessage(pattern=r"^/new$")) | |
async def new(event): | |
async with aiohttp.ClientSession() as session: | |
async with session.get(trivia_url) as resp: | |
results = await resp.json() | |
message = "category : " + results["results"][0]["category"] + "\ndifficulty : " + \ | |
results["results"][0]["difficulty"] + "\nquestion : " + \ | |
results["results"][0]["question"] | |
buttons = [] | |
buttons.append(Button.inline(results["results"][0]["correct_answer"], b"correct")) | |
for incorrect_answer in results["results"][0]["incorrect_answers"]: | |
buttons.append(Button.inline(incorrect_answer, data=b"incorrect")) | |
random.shuffle(buttons) | |
buttons = [buttons[i:i + 2] for i in range(0, len(buttons), 2)] | |
await event.reply(html.unescape(message), buttons=buttons) | |
@client.on(events.CallbackQuery) | |
async def callback(event): | |
print(event) | |
data = event.data | |
if data == b"correct": | |
await event.answer("Correct") | |
print("Correct") | |
elif data == b"incorrect": | |
await event.answer("Incorrect") | |
print("Incorrect") | |
await event.reply("Thank you for choosing") | |
client.run_until_disconnected() |