1
+ const Alice = require ( 'yandex-dialogs-sdk' )
2
+ const alice = new Alice ( )
3
+
4
+ const random = ( min , max ) => Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min
5
+
6
+ const { button, reply } = Alice
7
+ alice . welcome ( ctx => {
8
+ const replyMsg = reply ( {
9
+ text : 'Привет! Я загадала число от 1 до 100. Сможешь отгадать его?' ,
10
+ buttons : [ button ( 'Давай попробуем!' ) , button ( 'Не хочу' ) ]
11
+ } )
12
+ ctx . reply ( replyMsg )
13
+ } )
14
+
15
+ alice . command ( 'ДАВАЙ ПОПРОБУЕМ' , ctx => {
16
+ ctx . state . guessedNumber = random ( 1 , 100 )
17
+ ctx . reply ( 'Ну попробуй!' )
18
+ } )
19
+ alice . command ( 'Сыграть ещё раз!' , ctx => {
20
+ ctx . state . guessedNumber = random ( 1 , 100 )
21
+ ctx . reply ( 'Ну попробуй!' )
22
+ } )
23
+ alice . command ( 'Не хочу' , ctx => ctx . reply ( 'Спасибо за игру!' ) )
24
+
25
+ alice . command ( / ^ \d + $ / , ctx => {
26
+ const number = Number ( ctx . message )
27
+ if ( number > ctx . state . guessedNumber ) {
28
+ ctx . reply ( `Моё число меньше, чем ${ number } ` )
29
+ } else if ( number < ctx . state . guessedNumber ) {
30
+ ctx . reply ( `Моё число больше, чем ${ number } ` )
31
+ } else {
32
+ const replyMsg = reply ( {
33
+ text : `Ты победил! Я загадала число ${ ctx . state . guessedNumber } . Сыграешь ещё раз?` ,
34
+ buttons : [ button ( 'Сыграть ещё раз!' ) , button ( 'Не хочу' ) ]
35
+ } )
36
+ ctx . reply ( replyMsg )
37
+ }
38
+ } )
39
+
40
+ alice . any ( ctx => {
41
+ ctx . reply ( 'Ты ввёл вообще не число. Попробуй ещё раз!' )
42
+ } )
43
+
44
+ const port = 3000
45
+ alice . listen ( '/' , port , callback => console . log ( port ) )
0 commit comments