|
|
@@ -296,78 +296,33 @@ class Config extends Controller
|
|
|
* @apiParam {String} video_caption 视频文案
|
|
|
* @apiParam {Boolean} [isSend=true] 是否发送
|
|
|
* @apiParam {Boolean} [isTop=true] 是否置顶
|
|
|
- * @apiExample {js} button 示例
|
|
|
- * //button 的数据格式如下
|
|
|
- * [
|
|
|
- * [ //第一行
|
|
|
- * { //第一行按钮 的第一个按钮
|
|
|
- * "text": "百度", //按钮文字
|
|
|
- * "url": "https://baidu.com" //按钮跳转的链接
|
|
|
- * },
|
|
|
- * { //第一行按钮 的第二个按钮
|
|
|
- * "text": "百度",
|
|
|
- * "url": "https://baidu.com"
|
|
|
- * }
|
|
|
- * //更多按钮...
|
|
|
- * ],
|
|
|
- * [ //第二行
|
|
|
- * {
|
|
|
- * "text": "百度",
|
|
|
- * "url": "https://baidu.com"
|
|
|
- * }
|
|
|
- * ]
|
|
|
- * //更多行...
|
|
|
- * ]
|
|
|
*/
|
|
|
public function sendChannelMessage()
|
|
|
{
|
|
|
set_time_limit(0);
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
- $type = request()->input('type', 'image');
|
|
|
- if ($type == 'image') {
|
|
|
- request()->validate([
|
|
|
- 'chatId' => ['required', 'string', 'min:1'],
|
|
|
- 'image' => ['required', 'url'],
|
|
|
- 'text' => ['nullable', 'string'],
|
|
|
- 'button' => ['required', 'array'],
|
|
|
- 'button.*' => ['array'],
|
|
|
- 'button.*.*.text' => ['required', 'string'],
|
|
|
- 'button.*.*.url' => ['required', 'url'],
|
|
|
- 'isSend' => ['nullable', 'boolean'],
|
|
|
- 'isTop' => ['nullable', 'boolean'],
|
|
|
- ]);
|
|
|
-
|
|
|
-
|
|
|
- } else {
|
|
|
- request()->validate([
|
|
|
- 'chatId' => ['required', 'string', 'min:1'],
|
|
|
- 'video' => ['required', 'url'],
|
|
|
- 'video_caption' => ['nullable', 'string'],
|
|
|
- 'isSend' => ['nullable', 'boolean'],
|
|
|
- 'isTop' => ['nullable', 'boolean'],
|
|
|
- ]);
|
|
|
- }
|
|
|
- $chatId = request()->input('chatId');
|
|
|
- $image = request()->input('image');
|
|
|
- $button = request()->input('button');
|
|
|
- $text = request()->input('text');
|
|
|
- $isSend = request()->input('isSend', true);
|
|
|
- $isTop = request()->input('isTop', true);
|
|
|
|
|
|
- $video = request()->input('video');
|
|
|
- $video_caption = request()->input('video_caption');
|
|
|
- ConfigModel::where('field', 'channel_message')
|
|
|
- ->update([
|
|
|
- 'val' => json_encode([
|
|
|
- 'chatId' => $chatId,
|
|
|
- 'image' => $image,
|
|
|
- 'video' => $video,
|
|
|
- 'video_caption' => $video_caption,
|
|
|
- 'text' => $text,
|
|
|
- 'button' => $button
|
|
|
- ])
|
|
|
- ]);
|
|
|
+ $validate = [
|
|
|
+ 'chatId' => ['required', 'string', 'min:1'],
|
|
|
+ 'type' => ['required', 'string', 'in:image,video,text'],
|
|
|
+ 'text' => ['nullable', 'string'],
|
|
|
+ 'isSend' => ['nullable', 'boolean'],
|
|
|
+ 'isTop' => ['nullable', 'boolean'],
|
|
|
+ 'button' => ['array'],
|
|
|
+ 'button.*' => ['required', 'array'],
|
|
|
+ 'button.*.*.text' => ['required', 'string'],
|
|
|
+ 'button.*.*.url' => ['required', 'url'],
|
|
|
+ ];
|
|
|
+ $type = request()->input('type');
|
|
|
+ if (in_array($type, ['image', 'video'])) $validate[$type] = ['required', 'url'];
|
|
|
+ $params = request()->validate($validate);
|
|
|
+ $params['image'] = request()->input('image', '');
|
|
|
+ $params['video'] = request()->input('video', '');
|
|
|
+ $isSend = request()->input('isSend', false);
|
|
|
+ $isTop = request()->input('isTop', false);
|
|
|
+ unset($params['isTop'], $params['isSend']);
|
|
|
+ ConfigModel::where('field', 'channel_message')->update(['val' => json_encode($params)]);
|
|
|
DB::commit();
|
|
|
} catch (ValidationException $e) {
|
|
|
DB::rollBack();
|
|
|
@@ -379,78 +334,48 @@ class Config extends Controller
|
|
|
|
|
|
if ($isSend) {
|
|
|
try {
|
|
|
- $config = ConfigModel::where('field', 'channel_message')
|
|
|
- ->first()->val;
|
|
|
+ $config = ConfigModel::where('field', 'channel_message')->first()->val;
|
|
|
$config = json_decode($config, true);
|
|
|
$telegram = new Api(config('services.telegram.token'));
|
|
|
- if ($type == 'image') {
|
|
|
- // 发送图片消息
|
|
|
- $response = $telegram->sendPhoto([
|
|
|
- 'chat_id' => "@{$config['chatId']}",
|
|
|
- 'photo' => InputFile::create($config['image']),
|
|
|
- 'caption' => $config['text'],
|
|
|
- 'protect_content' => false,
|
|
|
- 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
|
|
|
- ]);
|
|
|
- } else {
|
|
|
- // 发送视频消息
|
|
|
- $response = $telegram->sendVideo([
|
|
|
- 'chat_id' => "@{$config['chatId']}",
|
|
|
- 'video' => InputFile::create($config['video']),
|
|
|
- 'caption' => $config['video_caption'],
|
|
|
- 'protect_content' => false
|
|
|
- // 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
|
|
|
- ]);
|
|
|
+ $msg = [];
|
|
|
+ $msg['chat_id'] = "@{$config['chatId']}";
|
|
|
+ $msg['protect_content'] = false;
|
|
|
+ if (!empty($config['button'])) {
|
|
|
+ $msg['reply_markup'] = json_encode(['inline_keyboard' => $config['button']]);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ switch ($type) {
|
|
|
+ case 'image':
|
|
|
+ $msg['photo'] = InputFile::create($config['image']);
|
|
|
+ $msg['caption'] = $config['text'];
|
|
|
+ $response = $telegram->sendPhoto($msg);
|
|
|
+ break;
|
|
|
+ case 'video':
|
|
|
+ $msg['video'] = InputFile::create($config['video']);
|
|
|
+ $msg['caption'] = $config['text'];
|
|
|
+ $response = $telegram->sendVideo($msg);
|
|
|
+ break;
|
|
|
+ case 'text':
|
|
|
+ $msg['text'] = $config['text'];
|
|
|
+ $response = $telegram->sendMessage($msg);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception("保存成功,发送失败", HttpStatus::CUSTOM_ERROR);
|
|
|
+ }
|
|
|
+ // 置顶消息
|
|
|
if ($isTop) {
|
|
|
- // 获取消息ID
|
|
|
$messageId = $response->get('message_id');
|
|
|
- // 置顶消息
|
|
|
- $telegram->pinChatMessage([
|
|
|
- 'chat_id' => "@{$config['chatId']}",
|
|
|
- 'message_id' => $messageId
|
|
|
- ]);
|
|
|
+ $telegram->pinChatMessage(['chat_id' => "@{$config['chatId']}", 'message_id' => $messageId]);
|
|
|
}
|
|
|
} catch (TelegramSDKException $e) {
|
|
|
- return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败');
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败', $e->getMessage());
|
|
|
} catch (Exception $e) {
|
|
|
- return $this->error(intval($e->getCode()), '保存成功,发送失败');
|
|
|
+ return $this->error($e->getCode(), '保存成功,发送失败');
|
|
|
}
|
|
|
}
|
|
|
return $this->success();
|
|
|
}
|
|
|
|
|
|
-// public function sendChannelVideo()
|
|
|
-// {
|
|
|
-// $chatId = request()->input('chatId');
|
|
|
-// $video = request()->input('video');
|
|
|
-// // $config = ConfigModel::where('field', 'channel_message')
|
|
|
-// // ->first()->val;
|
|
|
-// // $config = json_decode($config, true);
|
|
|
-// $telegram = new Api(config('services.telegram.token'));
|
|
|
-// // 发送图片消息
|
|
|
-// $response = $telegram->sendVideo([
|
|
|
-// 'chat_id' => "@{$chatId}",
|
|
|
-// 'caption' => '这是一个视频消息',
|
|
|
-// 'video' => InputFile::create($video),
|
|
|
-// 'protect_content' => false,
|
|
|
-// ]);
|
|
|
-
|
|
|
-// // 获取消息ID
|
|
|
-// $messageId = $response->get('message_id');
|
|
|
-// // 获取消息ID
|
|
|
-// $messageId = $response->get('message_id');
|
|
|
-// // 置顶消息
|
|
|
-// $telegram->pinChatMessage([
|
|
|
-// 'chat_id' => "@{$chatId}",
|
|
|
-// 'message_id' => $messageId
|
|
|
-// ]);
|
|
|
-// return $this->success($messageId);
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* @api {post} /admin/config/set 修改配置
|
|
|
* @apiGroup 配置
|