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
|
|
|
$config = $this->getParameter("buktopuha.config"); |
58
|
|
|
|
59
|
|
|
if (empty($config['webhook']['update_receiver'])) { |
60
|
|
|
throw new Exception("'webhook.update_receiver' for $secret is not valud service name", 0); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$updateReceiver = $this->getUpdateReceiverService($config['webhook']['update_receiver']); |
64
|
|
|
|
65
|
|
|
$update = new Update($data); |
66
|
|
|
|
67
|
|
|
$updateReceiver->handleUpdate($update); |
68
|
|
|
|
69
|
|
|
return new JsonResponse([ |
70
|
|
|
'ok' => true |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @return UpdateReceiverInterface |
77
|
|
|
*/ |
78
|
|
|
protected function getUpdateReceiverService($serviceName) |
79
|
|
|
{ |
80
|
|
|
return $this->container |
81
|
|
|
->get($serviceName); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|