cashwarden /
api
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace app\modules\v1\controllers; |
||||
| 4 | |||||
| 5 | use app\core\helpers\ArrayHelper; |
||||
| 6 | use app\core\services\TelegramService; |
||||
| 7 | use app\core\traits\ServiceTrait; |
||||
| 8 | use app\core\types\AuthClientType; |
||||
| 9 | use app\core\types\RecordSource; |
||||
| 10 | use app\core\types\TelegramKeyword; |
||||
| 11 | use TelegramBot\Api\BotApi; |
||||
| 12 | use TelegramBot\Api\Types\CallbackQuery; |
||||
| 13 | use TelegramBot\Api\Types\Message; |
||||
| 14 | use TelegramBot\Api\Types\ReplyKeyboardMarkup; |
||||
| 15 | use TelegramBot\Api\Types\Update; |
||||
| 16 | use yiier\graylog\Log; |
||||
| 17 | use yiier\helpers\StringHelper; |
||||
| 18 | |||||
| 19 | class TelegramController extends ActiveController |
||||
| 20 | { |
||||
| 21 | use ServiceTrait; |
||||
| 22 | |||||
| 23 | public $modelClass = ''; |
||||
| 24 | public $noAuthActions = ['hook', 'bind']; |
||||
| 25 | |||||
| 26 | public function actions() |
||||
| 27 | { |
||||
| 28 | $actions = parent::actions(); |
||||
| 29 | // 注销系统自带的实现方法 |
||||
| 30 | unset($actions['update'], $actions['index'], $actions['delete'], $actions['create']); |
||||
| 31 | return $actions; |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * @throws \Exception |
||||
| 36 | */ |
||||
| 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) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 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) { |
||||
|
0 ignored issues
–
show
|
|||||
| 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) { |
||||
|
0 ignored issues
–
show
|
|||||
| 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); |
||||
|
0 ignored issues
–
show
It seems like
$user can also be of type array and null; however, parameter $user of app\core\services\TelegramService::bind() does only seem to accept app\core\models\User, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 | } |
||||
| 189 | } |
||||
| 190 |