1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chrl\AppBundle\UpdateReceiver; |
4
|
|
|
|
5
|
|
|
use Chrl\AppBundle\BuktopuhaBotApi; |
6
|
|
|
use Chrl\AppBundle\Entity\Message; |
7
|
|
|
use Chrl\AppBundle\GameAction\CommandPool; |
8
|
|
|
use Chrl\AppBundle\GameAction\GameActionInterface; |
9
|
|
|
use Chrl\AppBundle\Service\GameService; |
10
|
|
|
use Chrl\AppBundle\Type\Update; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
|
13
|
|
|
class UpdateReceiver implements UpdateReceiverInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $config; |
17
|
|
|
private $telegramBotApi; |
18
|
|
|
private $entityManager; |
19
|
|
|
|
20
|
|
|
private $gameService; |
21
|
|
|
private $commandPool; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
BuktopuhaBotApi $telegramBotApi, |
25
|
|
|
$config, |
26
|
|
|
EntityManagerInterface $entityManager, |
27
|
|
|
GameService $gameService, |
28
|
|
|
CommandPool $commandPool |
29
|
|
|
) { |
30
|
|
|
|
31
|
|
|
$this->telegramBotApi = $telegramBotApi; |
32
|
|
|
$this->config = $config; |
33
|
|
|
$this->entityManager = $entityManager; |
34
|
|
|
$this->gameService = $gameService; |
35
|
|
|
$this->commandPool = $commandPool; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function handleUpdate(Update $update) |
39
|
|
|
{ |
40
|
|
|
$message = json_decode(json_encode($update->message), true); |
41
|
|
|
file_put_contents('/tmp/messages.tg.bot', json_encode($message), FILE_APPEND); |
42
|
|
|
|
43
|
|
|
$user = $this->gameService->getCurrentUser($message); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
if (isset($message['text'])) { |
47
|
|
|
$gameAction = $this->commandPool->setGameService($this->gameService)->findAction($message); |
48
|
|
|
|
49
|
|
|
$msg = new Message(); |
50
|
|
|
$msg->user = $user; |
51
|
|
|
$msg->date = new \DateTime("now"); |
52
|
|
|
$msg->text = $message['text']; |
53
|
|
|
$msg->game = $this->gameService->findGame($message); |
54
|
|
|
|
55
|
|
|
$this->gameService->em->persist($msg); |
56
|
|
|
$this->gameService->em->flush(); |
57
|
|
|
|
58
|
|
|
if (!$gameAction instanceof GameActionInterface) { |
59
|
|
|
$this->gameService->checkAnswer($message); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$gameAction->run($message, $user); |
64
|
|
|
} else { |
65
|
|
|
// handle possible joins/lefts |
66
|
|
|
$this->handleJoin($message); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function handleJoin($message) |
71
|
|
|
{ |
72
|
|
|
if (isset($message['new_chat_participant'])) { |
73
|
|
|
if ($message['new_chat_participant']['username'] == $this->config['bot_name']) { |
74
|
|
|
$this->telegramBotApi->sendMessage( |
75
|
|
|
$message['chat']['id'], |
76
|
|
|
'Thank you for inviting me to the group! Press /start to start the game!' |
77
|
|
|
); |
78
|
|
|
} else { |
79
|
|
|
$this->telegramBotApi->sendMessage( |
80
|
|
|
$message['chat']['id'], |
81
|
|
|
'Welcome to the group, @'. |
82
|
|
|
$message['new_chat_participant']['username'].'! Feel free to join the game!' |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|