1+ /// <reference path="../jsx.d.ts" />
2+ import React , { useState } from 'react' ;
3+ import { MtcuteAdapter } from '../mtcute-adapter' ;
4+
5+ // Quiz questions
6+ const questions = [
7+ { question : "What is 2 + 2?" , answer : "4" } ,
8+ { question : "What is the capital of France?" , answer : "paris" } ,
9+ { question : "What color is the sky?" , answer : "blue" }
10+ ] ;
11+
12+ const QuizBot : React . FC = ( ) => {
13+ const [ currentQuestion , setCurrentQuestion ] = useState ( 0 ) ;
14+ const [ score , setScore ] = useState ( 0 ) ;
15+ const [ waitingForAnswer , setWaitingForAnswer ] = useState ( true ) ;
16+ const [ lastAnswer , setLastAnswer ] = useState < string | null > ( null ) ;
17+ const [ gameOver , setGameOver ] = useState ( false ) ;
18+
19+ const handleAnswer = ( text : string ) => {
20+ if ( ! waitingForAnswer || gameOver ) return ;
21+
22+ setWaitingForAnswer ( false ) ;
23+ setLastAnswer ( text ) ;
24+
25+ const isCorrect = text . toLowerCase ( ) . trim ( ) === questions [ currentQuestion ] ?. answer ;
26+ if ( isCorrect ) {
27+ setScore ( score + 1 ) ;
28+ }
29+
30+ // Move to next question after a short delay
31+ setTimeout ( ( ) => {
32+ if ( currentQuestion < questions . length - 1 ) {
33+ setCurrentQuestion ( currentQuestion + 1 ) ;
34+ setWaitingForAnswer ( true ) ;
35+ setLastAnswer ( null ) ;
36+ } else {
37+ setGameOver ( true ) ;
38+ }
39+ } , 100 ) ;
40+ } ;
41+
42+ const restart = ( ) => {
43+ setCurrentQuestion ( 0 ) ;
44+ setScore ( 0 ) ;
45+ setWaitingForAnswer ( true ) ;
46+ setLastAnswer ( null ) ;
47+ setGameOver ( false ) ;
48+ } ;
49+
50+ if ( gameOver ) {
51+ return (
52+ < >
53+ < b > 🎉 Quiz Complete!</ b >
54+ { '\n\n' }
55+ Your final score: < b > { score } /{ questions . length } </ b >
56+ { '\n\n' }
57+ { score === questions . length ?
58+ "Perfect score! Well done! 🌟" :
59+ score >= questions . length / 2 ?
60+ "Good job! 👍" :
61+ "Better luck next time! 📚"
62+ }
63+ { '\n\n' }
64+ < row >
65+ < button onClick = { restart } > Play Again</ button >
66+ </ row >
67+ </ >
68+ ) ;
69+ }
70+
71+ const currentQ = questions [ currentQuestion ] ;
72+
73+ return (
74+ < >
75+ < b > Quiz Bot 🤖</ b >
76+ { '\n' }
77+ Question { currentQuestion + 1 } of { questions . length }
78+ { '\n' }
79+ Score: { score } /{ currentQuestion }
80+ { '\n\n' }
81+
82+ < b > { currentQ ?. question } </ b >
83+ { '\n\n' }
84+
85+ { lastAnswer !== null && (
86+ < >
87+ Your answer: < code > { lastAnswer } </ code >
88+ { '\n' }
89+ { lastAnswer . toLowerCase ( ) . trim ( ) === currentQ ?. answer ?
90+ "✅ Correct!" :
91+ `❌ Wrong! The answer was: ${ currentQ ?. answer } `
92+ }
93+ { '\n\n' }
94+ { currentQuestion < questions . length - 1 && "Next question coming up..." }
95+ { '\n' }
96+ </ >
97+ ) }
98+
99+ { waitingForAnswer && ! lastAnswer && (
100+ < >
101+ < i > Reply to this message with your answer!</ i >
102+ { '\n' }
103+ </ >
104+ ) }
105+
106+ { /* Input handler for answers */ }
107+ { waitingForAnswer && < input onSubmit = { handleAnswer } /> }
108+ </ >
109+ ) ;
110+ } ;
111+
112+ // Set up the bot
113+ async function main ( ) {
114+ const adapter = new MtcuteAdapter ( {
115+ apiId : parseInt ( process . env . API_ID ! ) ,
116+ apiHash : process . env . API_HASH ! ,
117+ botToken : process . env . BOT_TOKEN !
118+ } ) ;
119+
120+ // Register the quiz command
121+ adapter . onCommand ( 'quiz' , ( ) => < QuizBot /> ) ;
122+
123+ // Start the bot
124+ await adapter . start ( process . env . BOT_TOKEN ! ) ;
125+ console . log ( 'Quiz bot is running! Send /quiz to start.' ) ;
126+ }
127+
128+ main ( ) . catch ( console . error ) ;
0 commit comments