Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@painor
Last active March 21, 2024 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save painor/2531657c1e44386ce408dd063751d5dd to your computer and use it in GitHub Desktop.
Save painor/2531657c1e44386ce408dd063751d5dd to your computer and use it in GitHub Desktop.
Trivia bot telethon

Trivia bot

A small trivia bot made for my yotube tutorial

If anything wasn't clear you can PM me at @painor

بوت بسيط تم صنعه من اجل فيديو اليوتيب التعليمي

اذا كان شيء غير واضح يمكنك التواصل معي في التلغرام على المعرف التالي @painor

https://youtu.be/QaBwVeiQDQc

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment