|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\AnswerInlineQuery; |
|
7
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage; |
|
8
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Inline\Query; |
|
9
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\InputMessageContent\Text; |
|
10
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Message; |
|
11
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Update; |
|
12
|
|
|
use unreal4u\TelegramAPI\TgLog; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @todo refactor |
|
16
|
|
|
*/ |
|
17
|
|
|
class IncomingUpdateProcessor |
|
18
|
|
|
{ |
|
19
|
|
|
const CHAT_TYPE_PRIVATE = 'private'; |
|
20
|
|
|
const CHAT_TYPE_GROUP = 'group'; |
|
21
|
|
|
|
|
22
|
|
|
const PARSE_MODE_MARKDOWN = 'Markdown'; |
|
23
|
|
|
const PARSE_MODE_HTML5 = 'HTML'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TgLog |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var EntityManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $em; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \Twig_Environment |
|
37
|
|
|
*/ |
|
38
|
|
|
private $twig; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
private $pointUserId; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param TgLog $client |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(int $pointUserId, TgLog $client, EntityManagerInterface $em, \Twig_Environment $twig) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->client = $client; |
|
51
|
|
|
$this->em = $em; |
|
52
|
|
|
$this->twig = $twig; |
|
53
|
|
|
$this->pointUserId = $pointUserId; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Processes update and delegates it to corresponding service |
|
58
|
|
|
* |
|
59
|
|
|
* @param Update $update |
|
60
|
|
|
*/ |
|
61
|
|
|
public function process(Update $update) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($update->message && $update->message instanceof Message) { |
|
64
|
|
|
$chatType = $update->message->chat->type; |
|
65
|
|
|
|
|
66
|
|
|
if (self::CHAT_TYPE_PRIVATE === $chatType) { |
|
67
|
|
|
$this->processPrivateMessage($update); |
|
68
|
|
|
} elseif (self::CHAT_TYPE_GROUP === $chatType) { |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} elseif ($update->inline_query && $update->inline_query instanceof Query) { |
|
72
|
|
|
$this->processInlineQuery($update); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @todo refactor |
|
79
|
|
|
* |
|
80
|
|
|
* @param Update $update |
|
81
|
|
|
*/ |
|
82
|
|
|
private function processPrivateMessage(Update $update) |
|
83
|
|
|
{ |
|
84
|
|
|
$chatId = $update->message->chat->id; |
|
85
|
|
|
$text = $update->message->text; |
|
86
|
|
|
|
|
87
|
|
|
$sendMessage = new SendMessage(); |
|
88
|
|
|
$sendMessage->chat_id = $chatId; |
|
89
|
|
|
$sendMessage->parse_mode = self::PARSE_MODE_MARKDOWN; |
|
90
|
|
|
$sendMessage->disable_web_page_preview = true; |
|
91
|
|
|
|
|
92
|
|
|
$words = explode(' ', $text, 3); |
|
93
|
|
|
|
|
94
|
|
|
if (0 === count($words)) { |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
switch ($words[0]) { |
|
99
|
|
|
case 'l': |
|
100
|
|
|
case '/last': |
|
101
|
|
|
case 'last': |
|
102
|
|
|
if (array_key_exists(1, $words)) { |
|
103
|
|
|
$sendMessage->text = 'Not implemented yet :('; |
|
104
|
|
|
} else { |
|
105
|
|
|
$events = $this->em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastSubscriptionEvents(10); |
|
106
|
|
|
$sendMessage->text = $this->twig->render('@SkobkinPointTools/Telegram/last_global_subscriptions.md.twig', ['events' => $events]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
break; |
|
110
|
|
|
|
|
111
|
|
|
case 'sub': |
|
112
|
|
|
case '/sub': |
|
113
|
|
|
case 'subscribers': |
|
114
|
|
|
$sendMessage->text = 'Subscribers list here...'; |
|
115
|
|
|
break; |
|
116
|
|
|
|
|
117
|
|
|
case 'stats': |
|
118
|
|
|
case '/stats': |
|
119
|
|
|
$stats = [ |
|
120
|
|
|
'total_users' => $this->em->getRepository('SkobkinPointToolsBundle:User')->getUsersCount(), |
|
121
|
|
|
'active_users' => $this->em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->pointUserId), |
|
122
|
|
|
'today_events' => $this->em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(), |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
$sendMessage->text = $this->twig->render('@SkobkinPointTools/Telegram/stats.md.twig', $stats); |
|
126
|
|
|
|
|
127
|
|
|
break; |
|
128
|
|
|
|
|
129
|
|
|
case '/help': |
|
130
|
|
|
default: |
|
131
|
|
|
$sendMessage->text = $this->twig->render('@SkobkinPointTools/Telegram/help.md.twig'); |
|
132
|
|
|
break; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->client->performApiRequest($sendMessage); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function processInlineQuery(Update $update) |
|
139
|
|
|
{ |
|
140
|
|
|
$queryId = $update->inline_query->id; |
|
141
|
|
|
$text = $update->inline_query->query; |
|
142
|
|
|
|
|
143
|
|
|
if (mb_strlen($text) < 2) { |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$answerInlineQuery = new AnswerInlineQuery(); |
|
148
|
|
|
$answerInlineQuery->inline_query_id = $queryId; |
|
149
|
|
|
|
|
150
|
|
|
foreach ($this->em->getRepository('SkobkinPointToolsBundle:User')->findUsersLikeLogin($text) as $user) { |
|
151
|
|
|
$article = new Query\Result\Article(); |
|
152
|
|
|
$article->title = $user->getLogin(); |
|
153
|
|
|
|
|
154
|
|
|
$contentText = new Text(); |
|
155
|
|
|
$contentText->message_text = sprintf( |
|
156
|
|
|
"@%s:\nName: %s\nSubscribers: %d", |
|
157
|
|
|
$user->getLogin(), |
|
158
|
|
|
$user->getName(), |
|
159
|
|
|
$user->getSubscribers()->count() |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
$article->input_message_content = $contentText; |
|
163
|
|
|
$article->id = md5($user->getId()); |
|
164
|
|
|
|
|
165
|
|
|
$answerInlineQuery->addResult($article); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$this->client->performApiRequest($answerInlineQuery); |
|
169
|
|
|
} |
|
170
|
|
|
} |
This check looks for the bodies of
elseifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elseifbodies can be removed. If you have an empty elseif but statements in theelsebranch, consider inverting the condition.