|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Buktopuha - Telegram bot for russian trivia game |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Symfony |
|
8
|
|
|
* @package Chrl_Buktopuha |
|
9
|
|
|
* @author Kirill Kholodilin <[email protected]> |
|
10
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
|
11
|
|
|
* @link https://take2.ru/ |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace Chrl\AppBundle\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use Chrl\AppBundle\Type\Update; |
|
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
17
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Telegram controller - shows homepage and status |
|
23
|
|
|
* |
|
24
|
|
|
* @category Symfony |
|
25
|
|
|
* @package Chrl_Buktopuha |
|
26
|
|
|
* @author Kirill Kholodilin <[email protected]> |
|
27
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html MIT |
|
28
|
|
|
* @link https://take2.ru/ |
|
29
|
|
|
*/ |
|
30
|
|
|
class TelegramController extends Controller |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
public function setupWebhookAction($secret) |
|
34
|
|
|
{ |
|
35
|
|
|
$api = $this->get("buktopuha.telegram_bot_api"); |
|
36
|
|
|
$config = $this->getParameter("buktopuha.config"); |
|
37
|
|
|
|
|
38
|
|
|
if (empty($config['webhook']['domain'])) { |
|
39
|
|
|
throw new Exception("buktopuha.webhook.domain' is not set in config.yml", 0); |
|
40
|
|
|
} |
|
41
|
|
|
$url = "https://" . $config['webhook']['domain'] . $config['webhook']['path_prefix'] . "/telegram-bot/update"; |
|
42
|
|
|
if (null !== $secret) { |
|
43
|
|
|
$url .= "/" . $secret; |
|
44
|
|
|
} |
|
45
|
|
|
$res = $api->setwebhook($url); |
|
46
|
|
|
return new JsonResponse([ |
|
47
|
|
|
'ok' => $res, |
|
48
|
|
|
'url' => $url |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function updateAction($secret, Request $request) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$data0 = $request->getContent(); |
|
55
|
|
|
|
|
56
|
|
|
$data = json_decode($data0); |
|
57
|
|
|
|
|
58
|
|
|
$api = $this->get("buktopuha.telegram_bot_api"); |
|
|
|
|
|
|
59
|
|
|
$config = $this->getParameter("buktopuha.config"); |
|
60
|
|
|
|
|
61
|
|
|
if (empty($config['webhook']['update_receiver'])) { |
|
62
|
|
|
throw new Exception("'webhook.update_receiver' is not valud service name", 0); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$updateReceiver = $this->getUpdateReceiverService($config['webhook']['update_receiver']); |
|
66
|
|
|
|
|
67
|
|
|
$update = new Update($data); |
|
68
|
|
|
|
|
69
|
|
|
$updateReceiver->handleUpdate($update); |
|
70
|
|
|
|
|
71
|
|
|
return new JsonResponse([ |
|
72
|
|
|
'ok' => true |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* @return UpdateReceiverInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getUpdateReceiverService($serviceName) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->container |
|
83
|
|
|
->get($serviceName); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.