一、说明
仅需简单的几段代码,就能让你的独角拥有Telegram上新、补货通知的功能。需要授权码,购买请点击这里。
二、安装步骤
if (!function_exists('telegram_send_message')) {
function telegram_send_message($content) {
try {
$data = array(
"auth_key"=>env("TG_AUTH_KEY"),
"bot_api_key"=>env("TG_BOT_KEY"),
"chat_id"=>env("TG_CHAT_ID"),
"content"=>$content
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '<https://notify.777365.xyz/notify/api/send_message>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$response = curl_exec($ch);
if (curl_errno($ch)) {
// echo 'cURL Error: ' . curl_error($ch);
}
// var_dump($response);
curl_close($ch);
} catch (Exception $e) {
}
}
}
if (!function_exists('telegram_notify_restock')) {
function telegram_notify_restock($goodsId, $goodsName, $goodsCount) {
$appUrl = env("APP_URL");
$content = <<<EOF
🔉补货通知:
👉商品名称: <a href="{$appUrl}/buy/{$goodsId}">{$goodsName}</a>
👉补货数量: {$goodsCount}
🛒下单地址:<a href="{$appUrl}/buy/{$goodsId}">点击这里去下单</a>
EOF;
telegram_send_message($content);
}
}
if (!function_exists('telegram_notify_newgoods')) {
function telegram_notify_newgoods($goodsName) {
$appUrl = env("APP_URL");
$content = <<<EOF
🔉新品上架:
👉商品名称: <a href="{$appUrl}">{$goodsName}</a>
🛒下单地址:<a href="{$appUrl}">点击这里去下单</a>
EOF;
telegram_send_message($content);
}
}
protected function form()
{
return Form::make(new Carmis(), function (Form $form) {
$form->display('id');
$form->select('goods_id')->options(
Goods::query()->where('type', Goods::AUTOMATIC_DELIVERY)->pluck('gd_name', 'id')
)->required();
$form->radio('status')
->options(CarmisModel::getStatusMap())
->default(CarmisModel::STATUS_UNSOLD);
$form->switch('is_loop')->default(false);
$form->textarea('carmi')->required();
$form->display('created_at');
$form->display('updated_at');
// 增加如下代码
$form->saving(function (Form $form) {
// telegram notify
if ($form->status == 1) {
$goodsInfo = Goods::query()->where(array('id'=>$form->goods_id))->first();
telegram_notify_restock($goodsInfo->id, $goodsInfo->gd_name, 1);
}
});
});
}
// 加入如下代码
$goodsInfo = Goods::query()->where(array('id'=>$input['goods_id']))->first();
$goodsCnt = count($carmisData);
telegram_notify_restock($goodsInfo->id, $goodsInfo->gd_name, $goodsCnt);
return $this
->response()
->success(admin_trans('carmis.rule_messages.import_carmis_success'))
->location('/carmis');
$form->creating(function(Form $form){
if ($form->is_open) {
telegram_notify_newgoods($form->gd_name);
}
});
TG_AUTH_KEY=购买的KEY
TG_BOT_KEY=在TGBotFather中申请的机器人key
TG_CHAT_ID=需要通知的chat_id