Conditions | 13 |
Paths | 12 |
Total Lines | 151 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 3 | Features | 4 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
37 | public function actionHook() |
||
38 | { |
||
39 | try { |
||
40 | $bot = TelegramService::newClient(); |
||
41 | |||
42 | $bot->callbackQuery(function (CallbackQuery $message) use ($bot) { |
||
43 | $bot->answerCallbackQuery($message->getId(), "Loading..."); |
||
44 | $user = $this->userService->getUserByClientId( |
||
45 | AuthClientType::TELEGRAM, |
||
46 | $message->getFrom()->getId() |
||
47 | ); |
||
48 | if ($user) { |
||
|
|||
49 | \Yii::$app->user->setIdentity($user); |
||
50 | $this->telegramService->callbackQuery($message, $bot); |
||
51 | } |
||
52 | }); |
||
53 | |||
54 | $bot->command(ltrim(TelegramKeyword::REPORT, '/'), function (Message $message) use ($bot) { |
||
55 | $keyboard = new ReplyKeyboardMarkup( |
||
56 | [[TelegramKeyword::TODAY, TelegramKeyword::YESTERDAY, TelegramKeyword::LAST_MONTH]], |
||
57 | true |
||
58 | ); |
||
59 | /** @var BotApi $bot */ |
||
60 | $bot->sendMessage($message->getChat()->getId(), '请选择统计范围', null, false, null, $keyboard); |
||
61 | }); |
||
62 | |||
63 | $bot->command(ltrim(TelegramKeyword::CMD, '/'), function (Message $message) use ($bot) { |
||
64 | $keyboard = new ReplyKeyboardMarkup( |
||
65 | [[TelegramKeyword::REPORT]], |
||
66 | true |
||
67 | ); |
||
68 | /** @var BotApi $bot */ |
||
69 | $bot->sendMessage($message->getChat()->getId(), '请选择指令', null, false, null, $keyboard); |
||
70 | }); |
||
71 | |||
72 | $bot->command(ltrim(TelegramKeyword::HELP, '/'), function (Message $message) use ($bot) { |
||
73 | $text = "我能做什么?\n /help - 查看帮助 \n /cmd - 列出所有指令 \n /start - 开始使用 \n \n 绑定账号成功之后发送文字直接记账"; |
||
74 | /** @var BotApi $bot */ |
||
75 | $bot->sendMessage($message->getChat()->getId(), $text); |
||
76 | }); |
||
77 | |||
78 | $bot->command('ping', function (Message $message) use ($bot) { |
||
79 | $keyboard = new ReplyKeyboardMarkup( |
||
80 | [["one", "two", "three"]], |
||
81 | true |
||
82 | ); // true for one-time keyboard |
||
83 | /** @var BotApi $bot */ |
||
84 | $bot->sendMessage($message->getChat()->getId(), 'pong!', null, false, null, $keyboard); |
||
85 | }); |
||
86 | |||
87 | $bot->command(ltrim(TelegramKeyword::START, '/'), function (Message $message) use ($bot) { |
||
88 | $text = "您还未绑定账号,请先访问「个人设置」中的「账号绑定」进行绑定账号,然后才能快速记账。"; |
||
89 | $user = $this->userService->getUserByClientId( |
||
90 | AuthClientType::TELEGRAM, |
||
91 | $message->getFrom()->getId() |
||
92 | ); |
||
93 | if ($user) { |
||
94 | $text = '欢迎回来👏'; |
||
95 | } |
||
96 | /** @var BotApi $bot */ |
||
97 | $bot->sendMessage($message->getChat()->getId(), $text); |
||
98 | }); |
||
99 | |||
100 | $bot->on(function (Update $Update) use ($bot) { |
||
101 | $message = $Update->getMessage(); |
||
102 | $user = $this->userService->getUserByClientId( |
||
103 | AuthClientType::TELEGRAM, |
||
104 | $message->getFrom()->getId() |
||
105 | ); |
||
106 | if ($user) { |
||
107 | \Yii::$app->user->setIdentity($user); |
||
108 | $type = ltrim($message->getText(), '/'); |
||
109 | $text = $this->telegramService->getReportTextByType($type); |
||
110 | } else { |
||
111 | $text = '请先绑定您的账号'; |
||
112 | } |
||
113 | /** @var BotApi $bot */ |
||
114 | $bot->sendMessage($message->getChat()->getId(), $text); |
||
115 | }, function (Update $message) { |
||
116 | $msg = $message->getMessage(); |
||
117 | $report = [TelegramKeyword::TODAY, TelegramKeyword::YESTERDAY, TelegramKeyword::LAST_MONTH]; |
||
118 | if ($msg && in_array($msg->getText(), $report)) { |
||
119 | return true; |
||
120 | } |
||
121 | return false; |
||
122 | }); |
||
123 | |||
124 | // $bot->on(function (Update $Update) use ($bot) { |
||
125 | // $message = $Update->getMessage(); |
||
126 | // /** @var BotApi $bot */ |
||
127 | // $bot->sendMessage($message->getChat()->getId(), "hi"); |
||
128 | // }, function (Update $message) { |
||
129 | // if ($message->getMessage() && $message->getMessage()->getText() == '/login') { |
||
130 | // return true; |
||
131 | // } |
||
132 | // return false; |
||
133 | // }); |
||
134 | |||
135 | $bot->on(function (Update $Update) use ($bot) { |
||
136 | $message = $Update->getMessage(); |
||
137 | $token = StringHelper::after(TelegramKeyword::BIND . '/', $message->getText()); |
||
138 | try { |
||
139 | $user = $this->userService->getUserByResetToken($token); |
||
140 | $this->telegramService->bind($user, $token, $message); |
||
141 | $text = '成功绑定账号【' . data_get($user, 'username') . '】!'; |
||
142 | } catch (\Exception $e) { |
||
143 | $text = $e->getMessage(); |
||
144 | } |
||
145 | /** @var BotApi $bot */ |
||
146 | $bot->sendMessage($message->getChat()->getId(), $text); |
||
147 | }, function (Update $message) { |
||
148 | $msg = $message->getMessage(); |
||
149 | if ($msg && strpos($msg->getText(), TelegramKeyword::BIND) === 0) { |
||
150 | return true; |
||
151 | } |
||
152 | return false; |
||
153 | }); |
||
154 | |||
155 | $bot->on(function (Update $Update) use ($bot) { |
||
156 | $message = $Update->getMessage(); |
||
157 | $keyboard = null; |
||
158 | try { |
||
159 | $user = $this->userService->getUserByClientId( |
||
160 | AuthClientType::TELEGRAM, |
||
161 | $message->getFrom()->getId() |
||
162 | ); |
||
163 | \Yii::$app->user->setIdentity($user); |
||
164 | $model = $this->transactionService->createByDesc($message->getText(), RecordSource::TELEGRAM); |
||
165 | $keyboard = $this->telegramService->getRecordMarkup($model); |
||
166 | $text = $this->telegramService->getMessageTextByTransaction($model); |
||
167 | } catch (\Exception $e) { |
||
168 | $text = $e->getMessage(); |
||
169 | } |
||
170 | /** @var BotApi $bot */ |
||
171 | $bot->sendMessage($message->getChat()->getId(), $text, null, false, null, $keyboard); |
||
172 | }, function (Update $message) { |
||
173 | if ($message->getMessage()) { |
||
174 | if (ArrayHelper::strPosArr($message->getMessage()->getText(), TelegramKeyword::items()) === 0) { |
||
175 | return false; |
||
176 | } |
||
177 | return true; |
||
178 | } |
||
179 | return false; |
||
180 | }); |
||
181 | |||
182 | $bot->run(); |
||
183 | } catch (\TelegramBot\Api\Exception $e) { |
||
184 | Log::error('webHook error' . $e->getMessage(), (string)$e); |
||
185 | throw $e; |
||
186 | } |
||
187 | return ''; |
||
188 | } |
||
190 |