首页 » IDC运维 » 独角发卡网2.0配置企业微信和电报telegram订单消息通知

独角发卡网2.0配置企业微信和电报telegram订单消息通知

 

独角发卡网2.0配置企业微信和电报telegram订单消息通知,tg_bot,电报机器人,订单通知,不用server酱

独角发卡网2.0配置企业微信和电报telegram订单消息通知,tg_bot,电报机器人,订单通知,不用server酱


[email protected]_xieyi提供,[email protected]_xieyi作出的贡献

说明:本程序改写了原有的server酱通知,文件名还是沿用了serverjiang.php(建议备份原有的文件)

配置起来也比较简单,将代码覆盖serverjiang.php的内容。然后在env里配置秘钥即可

代码位置:/app/Jobs

<?php

namespace App\Jobs;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class ServerJiang implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 2;

    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 30;

    /**
     * @var Order
     */
    private $order;

    /**
     * 商品服务层.
     * @var \App\Service\PayService
     */
    private $goodsService;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->goodsService = app('Service\GoodsService');

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
         $good_info =$this->goodsService->detail($this->order->goods_id);
         $good_stock=$good_info['in_stock'];

        $msg="- ". __('order.fields.title') .":{$this->order->title}
- 剩余库存:{$good_stock}
- 付款时间:{$this->order->updated_at}
- ". __('order.fields.order_sn') .":{$this->order->order_sn}
- ". __('order.fields.email') .":{$this->order->email}
- ". __('order.fields.actual_price') .":{$this->order->actual_price}";
        //发送企业微信
        $corpid=env('CORPID');
        $corpsecret=env('CORPSECRET');
        $agenid=env('AGENTID');
        if($corpid && $corpsecret && $agenid){
            $token_info=Redis::get('WXWORK_TOKEN');
            $token_info=$token_info==null?[]:json_decode($token_info,true);
//            $token=$token_info['access_token'];
            if($token_info)$token=$token_info['access_token'];
            if (!$token_info || !$token || !$token_info['expires_time'] ||$token_info['expires_time']<=time()) {
                   $gettoken=$this->get_workwx_token($corpid,$corpsecret);
                    Log::info('企业微信-获取token'.json_encode($gettoken));
                   $token=$gettoken->access_token;
                   $time=date('Y-m-d H:i:s');
                   Redis::set('WXWORK_TOKEN',json_encode(['access_token'=>$token,'expires_time'=>time()+$gettoken->expires_in]));
            }
            $parent=['touser'=>'@all','msgtype'=>'text','agentid'=>$agenid,'safe'=>0,
                'text'=>[
                    'content'=>$msg
                    ]
                ];
            $postdata=json_encode($parent);
            $res=$this->httppost('https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$token,$postdata);
            Log::info('企业微信-发送信息'.$res);
        }

        //发送TG
        $tg_token=env('TGBOT_TOKEN');
        $tg_chatid=env('TG_CHATID');
        if($tg_token && $tg_chatid){
            $postdata=[ 'chat_id'=>$tg_chatid,'text'=>$msg.PHP_EOL];
            $res= $this->httppost('https://api.telegram.org/bot'.$tg_token.'/sendMessage',$postdata);
             Log::info('TG-发送返回'.$res);
        }

    }

    /**
     * Execute the job.
     *获取企业微信token
     * @return void
     */ 
    public function get_workwx_token ($corpid,$corpsecret){
    $gettoken=$this->httppost("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpid."&corpsecret=".$corpsecret);
    $gettoken=json_decode($gettoken);
    $token=$gettoken->access_token;
    return $gettoken;
    }

    public function httppost($url,$data=null)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => $data,
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return $response;
    }
}

.env里追加(env在根目录)

#企业微信 
CORPID=
CORPSECRET=
AGENTID=

#TG通知 
TGBOT_TOKEN=
TG_CHATID=

配置后,重启一下应用管理器,测试一下是否OK
一定要记得将后台的server酱的开关打开哦。

原文链接:独角发卡网2.0配置企业微信和电报telegram订单消息通知,转载请注明来源!

0