|
1 | 1 | <?php |
2 | | - |
3 | 2 | namespace Kanboard\Plugin\Telegram\Controller; |
4 | 3 |
|
5 | 4 | use Kanboard\Controller\BaseController; |
|
10 | 9 | use Longman\TelegramBot\Exception\TelegramException; |
11 | 10 | use Kanboard\Core\Base; |
12 | 11 |
|
13 | | - |
14 | 12 | class TelegramController extends BaseController |
15 | 13 | { |
16 | 14 | public function get_user_chat_id() |
17 | 15 | { |
18 | 16 | $user = $this->getUser(); |
19 | 17 | //$this->checkCSRFParam(); |
20 | | - |
21 | 18 | $apikey = $this->userMetadataModel->get($user['id'], 'telegram_apikey', $this->configModel->get('telegram_apikey')); |
22 | 19 | $bot_username = $this->userMetadataModel->get($user['id'], 'telegram_username', $this->configModel->get('telegram_username')); |
23 | | - $offset = 0+$this->userMetadataModel->get($user['id'], 'telegram_offset', $this->configModel->get('telegram_offset')); |
24 | | - $private_message = mb_substr(urldecode($this->request->getStringParam('private_message')),0,32); |
| 20 | + $offset = 0 + $this->userMetadataModel->get($user['id'], 'telegram_offset', $this->configModel->get('telegram_offset')); |
| 21 | + $private_message = mb_substr(urldecode($this->request->getStringParam('private_message')), 0, 32); |
25 | 22 |
|
26 | 23 | list($offset, $chat_id, $user_name) = $this->get_chat_id($apikey, $bot_username, $offset, $private_message); |
27 | 24 |
|
28 | | - if($offset != 0){ |
| 25 | + if ($offset != 0) |
| 26 | + { |
29 | 27 | //ok |
30 | | - $this->userMetadataModel->save($user['id'], array('telegram_offset' => $offset) ); |
31 | | - $this->response->html($this->template->render('telegram:user/save_chat_id', array( |
32 | | - 'chat_id' => $chat_id, |
33 | | - 'user_name' => $user_name, |
34 | | - 'private_message' => $private_message, |
35 | | - 'bot_url' => "https://t.me/".$bot_username, |
36 | | - 'user' => $user |
37 | | - ))); |
38 | | - }else{ |
| 28 | + $this->userMetadataModel->save($user['id'], array('telegram_offset' => $offset)); |
| 29 | + $this->response->html($this->template->render('telegram:user/save_chat_id', array('chat_id' => $chat_id, 'user_name' => $user_name, 'private_message' => $private_message, 'bot_url' => "https://t.me/" . $bot_username, 'user' => $user))); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
39 | 33 | //error |
40 | | - $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true); |
| 34 | + $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'])), true); |
41 | 35 | } |
42 | 36 | } |
43 | 37 |
|
44 | 38 | public function get_project_chat_id() |
45 | 39 | { |
46 | 40 | $project = $this->getProject(); |
47 | 41 | //$this->checkCSRFParam(); |
48 | | - |
49 | 42 | $apikey = $this->projectMetadataModel->get($project['id'], 'telegram_apikey', $this->configModel->get('telegram_apikey')); |
50 | 43 | $bot_username = $this->projectMetadataModel->get($project['id'], 'telegram_username', $this->configModel->get('telegram_username')); |
51 | | - $offset = 0+$this->projectMetadataModel->get($project['id'], 'telegram_offset', $this->configModel->get('telegram_offset')); |
52 | | - $private_message = mb_substr(urldecode($this->request->getStringParam('private_message')),0,32); |
| 44 | + $offset = 0 + $this->projectMetadataModel->get($project['id'], 'telegram_offset', $this->configModel->get('telegram_offset')); |
| 45 | + $private_message = mb_substr(urldecode($this->request->getStringParam('private_message')), 0, 32); |
53 | 46 |
|
54 | 47 | list($offset, $chat_id, $user_name) = $this->get_chat_id($apikey, $bot_username, $offset, $private_message); |
55 | 48 |
|
56 | | - if($offset != 0){ |
| 49 | + if ($offset != 0) |
| 50 | + { |
57 | 51 | //ok |
58 | | - $this->projectMetadataModel->save($project['id'], array('telegram_offset' => $offset) ); |
59 | | - $this->response->html($this->template->render('telegram:project/save_chat_id', array( |
60 | | - 'chat_id' => $chat_id, |
61 | | - 'user_name' => $user_name, |
62 | | - 'private_message' => $private_message, |
63 | | - 'bot_url' => "https://t.me/".$bot_username, |
64 | | - 'project' => $project |
65 | | - ))); |
66 | | - }else{ |
| 52 | + $this->projectMetadataModel->save($project['id'], array('telegram_offset' => $offset)); |
| 53 | + $this->response->html($this->template->render('telegram:project/save_chat_id', array('chat_id' => $chat_id, 'user_name' => $user_name, 'private_message' => $private_message, 'bot_url' => "https://t.me/" . $bot_username, 'project' => $project))); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
67 | 57 | //error |
68 | | - $this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'] )), true); |
| 58 | + $this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'])), true); |
69 | 59 | } |
70 | 60 | } |
71 | 61 |
|
72 | 62 | private function get_chat_id($apikey, $bot_username, $offset, $private_message) |
73 | 63 | { |
74 | | - try |
75 | | - { |
76 | | - if(empty($private_message) || mb_strlen($private_message) != 32){ |
77 | | - throw new TelegramException("empty private_message!"); |
78 | | - } |
| 64 | + try |
| 65 | + { |
| 66 | + if (empty($private_message) || mb_strlen($private_message) != 32) |
| 67 | + { |
| 68 | + throw new TelegramException("empty private_message!"); |
| 69 | + } |
79 | 70 |
|
80 | | - // Create Telegram API object |
81 | | - $telegram = new TelegramClass($apikey, $bot_username); |
82 | | - |
83 | | - $limit=100; |
84 | | - $timeout = 1; |
85 | | - $response = Request::getUpdates( |
86 | | - [ |
87 | | - 'offset' => $offset+1, |
88 | | - 'limit' => $limit, |
89 | | - 'timeout' => $timeout, |
90 | | - ] |
91 | | - ); |
92 | | - |
93 | | - $chat_id=""; |
94 | | - $user_name=""; |
95 | | - |
96 | | - if ($response->isOk()) { |
97 | | - //Process all updates |
98 | | - /** @var Update $result */ |
99 | | - foreach ((array) $response->getResult() as $result) { |
100 | | - $offset = $result->getUpdateId(); |
101 | | - if( $result->getMessage() != NULL){ |
102 | | - if( $private_message === mb_substr(trim($result->getMessage()->getText()),0,32) ){ |
103 | | - $chat_id = $result->getMessage()->getChat()->getId(); |
104 | | - $user_name = $result->getMessage()->getChat()->getFirstName(); |
105 | | - break; |
106 | | - } |
| 71 | + // Create Telegram API object |
| 72 | + $telegram = new TelegramClass($apikey, $bot_username); |
| 73 | + |
| 74 | + $limit = 100; |
| 75 | + $timeout = 1; |
| 76 | + $response = Request::getUpdates(['offset' => $offset + 1, 'limit' => $limit, 'timeout' => $timeout, ]); |
| 77 | + |
| 78 | + $chat_id = ""; |
| 79 | + $user_name = ""; |
| 80 | + |
| 81 | + if ($response->isOk()) |
| 82 | + { |
| 83 | + //Process all updates |
| 84 | + |
| 85 | + /** @var Update $result */ |
| 86 | + foreach ((array)$response->getResult() as $result) |
| 87 | + { |
| 88 | + $offset = $result->getUpdateId(); |
| 89 | + if ($result->getMessage() != NULL) |
| 90 | + { |
| 91 | + if ($private_message === mb_substr(trim($result->getMessage()->getText()), 0, 32)) |
| 92 | + { |
| 93 | + $chat_id = $result->getMessage()->getChat()->getId(); |
| 94 | + $user_name = $result->getMessage()->getChat()->getFirstName(); |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + throw new TelegramException($response->printError(true)); |
107 | 103 | } |
108 | | - } |
109 | | - }else{ |
110 | | - throw new TelegramException($response->printError(true)); |
111 | 104 | } |
112 | | - } |
113 | | - catch (TelegramException $e) |
114 | | - { |
115 | | - // log telegram errors |
116 | | - error_log($e->getMessage()); |
117 | | - $this->flash->failure(t('Telegram error: ').$e->getMessage()); |
118 | | - return 0;//$this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true); |
119 | | - } |
120 | | - |
121 | | - return array($offset, $chat_id, $user_name); |
| 105 | + catch(TelegramException $e) |
| 106 | + { |
| 107 | + // log telegram errors |
| 108 | + error_log($e->getMessage()); |
| 109 | + $this->flash->failure(t('Telegram error: ') . $e->getMessage()); |
| 110 | + return 0; //$this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + return array($offset, $chat_id, $user_name); |
122 | 115 | } |
123 | 116 |
|
124 | | - public function save_user_chat_id(){ |
125 | | - $user = $this->getUser(); |
126 | | - $this->checkCSRFParam(); |
127 | | - |
128 | | - $chat_id = urldecode($this->request->getStringParam('chat_id')); |
129 | | - if(is_numeric($chat_id)){ |
130 | | - $this->userMetadataModel->save($user['id'], array('telegram_user_cid' => $chat_id) ); |
131 | | - $this->flash->success(t("Chat id was updated to %s",$chat_id)); |
132 | | - }else{ |
133 | | - $this->flash->failure(t('Telegram error: wrong chat id')); |
134 | | - } |
135 | | - return $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true); |
| 117 | + public function save_user_chat_id() |
| 118 | + { |
| 119 | + $user = $this->getUser(); |
| 120 | + $this->checkCSRFParam(); |
| 121 | + |
| 122 | + $chat_id = urldecode($this->request->getStringParam('chat_id')); |
| 123 | + if (is_numeric($chat_id)) |
| 124 | + { |
| 125 | + $this->userMetadataModel->save($user['id'], array('telegram_user_cid' => $chat_id)); |
| 126 | + $this->flash->success(t("Chat id was updated to %s", $chat_id)); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + $this->flash->failure(t('Telegram error: wrong chat id')); |
| 131 | + } |
| 132 | + return $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'])), true); |
136 | 133 | } |
137 | 134 |
|
138 | | - public function save_project_chat_id(){ |
139 | | - $project = $this->getProject(); |
140 | | - $this->checkCSRFParam(); |
141 | | - |
142 | | - $chat_id = urldecode($this->request->getStringParam('chat_id')); |
143 | | - if(is_numeric($chat_id)){ |
144 | | - $this->projectMetadataModel->save($project['id'], array('telegram_group_cid' => $chat_id) ); |
145 | | - $this->flash->success(t("Chat id was updated to %s",$chat_id)); |
146 | | - }else{ |
147 | | - $this->flash->failure(t('Telegram error: wrong chat id')); |
148 | | - } |
149 | | - return $this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'] )), true); |
| 135 | + public function save_project_chat_id() |
| 136 | + { |
| 137 | + $project = $this->getProject(); |
| 138 | + $this->checkCSRFParam(); |
| 139 | + |
| 140 | + $chat_id = urldecode($this->request->getStringParam('chat_id')); |
| 141 | + if (is_numeric($chat_id)) |
| 142 | + { |
| 143 | + $this->projectMetadataModel->save($project['id'], array('telegram_group_cid' => $chat_id)); |
| 144 | + $this->flash->success(t("Chat id was updated to %s", $chat_id)); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + $this->flash->failure(t('Telegram error: wrong chat id')); |
| 149 | + } |
| 150 | + return $this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'])), true); |
150 | 151 | } |
151 | 152 | } |
0 commit comments