|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; |
|
8
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\AnswerInlineQuery; |
|
9
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Inline\Query; |
|
10
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\InputMessageContent\Text; |
|
11
|
|
|
use unreal4u\TelegramAPI\TgLog; |
|
12
|
|
|
|
|
13
|
|
|
class InlineQueryProcessor |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityManagerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $em; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var UserRepository |
|
22
|
|
|
*/ |
|
23
|
|
|
private $userRepo; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TgLog |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
public function __construct(EntityManagerInterface $em, TgLog $client) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->em = $em; |
|
34
|
|
|
$this->client = $client; |
|
35
|
|
|
|
|
36
|
|
|
$this->userRepo = $em->getRepository('SkobkinPointToolsBundle:User'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function process(Query $inlineQuery) |
|
40
|
|
|
{ |
|
41
|
|
|
if (mb_strlen($inlineQuery->query) < 2) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$answerInlineQuery = new AnswerInlineQuery(); |
|
46
|
|
|
$answerInlineQuery->inline_query_id = $inlineQuery->id; |
|
47
|
|
|
|
|
48
|
|
|
foreach ($this->em->getRepository('SkobkinPointToolsBundle:User')->findUsersLikeLogin($inlineQuery->query) as $user) { |
|
49
|
|
|
$article = new Query\Result\Article(); |
|
50
|
|
|
$article->title = $user->getLogin(); |
|
51
|
|
|
|
|
52
|
|
|
$contentText = new Text(); |
|
53
|
|
|
$contentText->message_text = sprintf( |
|
54
|
|
|
"@%s:\nName: %s\nSubscribers: %d", |
|
55
|
|
|
$user->getLogin(), |
|
56
|
|
|
$user->getName(), |
|
57
|
|
|
$user->getSubscribers()->count() |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$article->input_message_content = $contentText; |
|
61
|
|
|
$article->id = md5($user->getId()); |
|
62
|
|
|
|
|
63
|
|
|
$answerInlineQuery->addResult($article); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->client->performApiRequest($answerInlineQuery); |
|
67
|
|
|
} |
|
68
|
|
|
} |