public function process() { if (mb_substr($this->message->Content, 0, 2) != '飞机') { return null; } $response = Event::fire('wechat.flight.pre', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $key = str_replace('飞机', '', $this->message->Content); list($from, $to) = explode('到', $key); $date = date('Y-m-d', strtotime('+1 days')); $url = 'http://touch.qunar.com/h5/flight/flightlist?'; $url = $url . 'startCity=' . $from . '&destCity=' . $to . '&startDate=' . $date; $response = Event::fire('wechat.flight.post', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $title = '机票查询'; $pic = 'http://img0.imgtn.bdimg.com/it/u=2756508793,1924774665&fm=21&gp=0.jpg'; $desc = '便民工具箱'; $this->result = Message::make('news')->items(function () use($title, $url, $desc, $pic) { return [Message::make('news_item')->title($title)->description($desc)->url($url)->picUrl($pic)]; }); }
public function news() { $news = Message::make('news')->items(function () { return array(Message::make('news_item')->title('测试标题'), Message::make('news_item')->title('测试标题2')->description('好不好?'), Message::make('news_item')->title('测试标题3')->description('好不好说句话?')->url('http://baidu.com'), Message::make('news_item')->title('测试标题4')->url('http://baidu.com/abc.php')->picUrl('http://mmbiz.qpic.cn/mmbiz/EWBwscfHhF9mWxDBqJSTfS3ByNEicPrQzicHS5RicGsPOSkuqib2SOxuPFEGr3wnvztCae58ps21TnQVKEJb2YBEaA/640?wx_fmt=jpeg&tp=webp&wxfrom=5')); }); var_dump($news); }
public function process() { /** * 触发条件,请自行修改 */ if (mb_substr($this->message->Content, 0, 2) != '空气') { return null; } $response = Event::fire('wechat.kong_qi.pre', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $word = trim(str_replace('空气', '', $this->message->Content)); $url = 'http://api.avatardata.cn/Aqi/LookUp?key=0eb92841968742699295a39f712e5a2e&city=' . $word; $result = $this->getRequest($url); $response = Event::fire('wechat.kong_qi.post', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } if (!isset($result['error_code']) || $result['error_code']) { $this->result = Message::make('text')->content('未找到该城市空气质量.'); return true; } $info = $result['result']; $data = ['污染指数:' . $info['aqi'], '空气质量:' . $info['level'], '主要污染物:' . $info['core'], '播报时间:' . date('Y-m-d H:i', strtotime($info['time']))]; $this->result = Message::make('text')->content(implode("\n", $data)); }
public function process() { /** * 触发条件,请自行修改 */ if (mb_substr($this->message->Content, 0, 2) != '游戏') { return null; } $response = Event::fire('wechat.game.pre', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $response = Event::fire('wechat.game.post', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $title = '微信游戏'; $url = 'http://' . $_SERVER['HTTP_HOST'] . '/game/kanshu'; $desc = '上百款微信小游戏'; $pic = 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3149524520,430835410&fm=58'; $this->result = Message::make('news')->items(function () use($title, $url, $desc, $pic) { return [Message::make('news_item')->title($title)->description($desc)->url($url)->picUrl($pic)]; }); }
static function autoReplyMessage($message) { $content = $message->Content; $reply_text = $content; if ($content == 'info') { $reply_text = '我是' . $message->ToUserName . PHP_EOL . '您好' . $message->FromUserName . '!'; } else { if (in_array($content, ['在', '在不', '在吗'])) { $reply_text = '指尖微校园有什么可以帮到您'; } else { if (in_array($content, ['早', '早上好'])) { $reply_text = '每天都是崭新的一天哦'; } else { if (in_array($content, ['晚安', 'night'])) { $reply_text = '祝美梦~~~'; } else { if (in_array($content, ['电话', '手机', '号码'])) { $reply_text = '欢迎咨询 400-611-8602'; } else { if (in_array($content, ['QQ', '聊天'])) { $reply_text = '欢迎交流 3273643062'; } else { $reply_text = <<<EOD 有任何问题需要咨询请在工作时间(周一~周日9:00~21:00)拨打我们的客服电话 400-611-8602,欢迎来电哟~[亲亲] EOD; } } } } } } return Message::make('text')->content($reply_text); }
public function process() { /** * 触发条件,请自行修改 */ if (mb_substr($this->message->Content, 0, 2) != '手机') { return null; } $response = Event::fire('wechat.mobile_region.pre', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $no = str_replace('手机', '', $this->message->Content); $url = 'http://apis.baidu.com/showapi_open_bus/mobile/find?num=' . trim($no); $result = $this->getRequest($url); $response = Event::fire('wechat.mobile_region.post', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } if (!isset($result['showapi_res_code']) || $result['showapi_res_code']) { $this->result = Message::make('text')->content('手机归属查询服务异常.'); return true; } $data = ['城市:' . $result['showapi_res_body']['city'], '名称:' . $result['showapi_res_body']['name']]; $this->result = Message::make('text')->content(implode("\n", $data)); }
/** * 准备消息. * * @param \Overtrue\Wechat\Messages\BaseMessage $message * * @return Broadcast */ public function send($message) { is_string($message) && ($message = Message::make('text')->with('content', $message)); if (!$message instanceof BaseMessage) { throw new \Exception("消息必须继承自 'Overtrue\\Wechat\\BaseMessage'"); } $this->message = $message; return $this; }
/** * Format a normal news reply message * * @param $replyBody * @return news message * */ public static function replyNews($arr) { $news = Message::make('news')->items(function () use($arr) { $items = array(); foreach ($arr as $key => $value) { $items[] = Message::make('news_item')->title($value['title'])->description($value['description'])->url($value['url'])->picUrl($value['picUrl']); } return $items; }); return $news; }
public function actionIndex() { $server = new Server(WxConfig::APP_ID, WxConfig::TOKEN); $server->on('message', function ($message) { if ($message['Content'] == '扫一扫') { return Message::make('text')->content('OpenId:' . $message['FromUserName'] . '将调起扫一扫事件http://weixin.wwhis.com'); } else { Message::make('text')->content('fail'); } }); $result = $server->serve(); // DXLog::debug('wx_server_result', ['data' => json_encode($result)]); echo $result; }
public function reply($message) { /* * 监听事件类型 * 关注事件回复 * */ //获取公众号信息 $public_number = $message->ToUserName; $wechat = Wechat::where('wechat_account', '=', $public_number)->firstOrFail(); //获取关键词对象 $message->Content; //查询关键字,预载入关键字规则 $keyword = Keyword::with(['keywordRule' => function ($query) use($wechat) { $query->where('wechat_id', '=', $wechat->id); }])->where('keyword', 'like', "{$message->Content}")->first(); //查询对应回复 一对多 $replies = $keyword->keywordRule->reply; //dd($replies); foreach ($replies as $key => $reply) { $contents[$key] = $reply->{$reply->reply_type}; $contents[$key]['reply_type'] = $reply->reply_type; } //取随机数 $num = mt_rand(0, count($replies)); $content = $contents[$num]; switch ($content['reply_type']) { case 'text': case 'image': case 'voice': case 'video': case 'location': return Message::make($content['reply_type'])->content($content->content); break; default: return Message::make($content['reply_type'])->content($content->content); break; case 'news': //查询内容 $news = WechatNews::find($content->content); return Message::make('news')->items(function () use($news) { return array(Message::make('news_item')->title($news->title)->url($news->news_url)->picUrl($news->cover)); }); breadk; } }
public function serve(Server $server) { $server->on('event', function ($event) { switch (strtolower($event->Event)) { case "subscribe": // 添加关注者信息 DB::table('member')->insert(['openid' => $event->FromUserName, 'token' => $event->ToUserName]); // 关注时候二维码key EventKey return Message::make('text')->content('感谢您关注'); break; case "unsubscribe": break; case "location": return Message::make('text')->content('定位!'); break; case "click": return Message::make('text')->content('点击事件!'); break; } }); // 监听所有类型 $server->on('message', function ($message) { switch (strtolower($message->MsgType)) { case "text": return Message::make('text')->content('您好!'); break; case "image": break; case "location": break; case "link": break; case "voice": break; case "music": break; case "video": break; case "news": break; } }); return $server->serve(); // 或者 return $server; }
/** * 处理微信的请求消息 * * @param Overtrue\Wechat\Server $server * * @return string */ public function serve() { $server = new Server($this->appId, $this->token, $this->encodingKey); if ('development' == $this->env) { $this->setWechatMenu(); } $server->on('message', function ($message) { Log::info('收到文字消息,关注者openid: ' . $message['FromUserName']); return "您好!欢迎关注 小叶测试号!"; }); // 只监听指定类型事件 $server->on('event', function ($event) { return Message::make('text')->content('感谢您关注小叶测试号!'); }); // Log::debug($server->serve()); // 您可以直接echo 或者返回给框架 return $server->serve(); }
/** * 处理微信的请求消息 * * @param Overtrue\Wechat\Server $server * * @return string */ public function serve() { $appId = config('wechat.appId'); $secret = config('wechat.secret'); $token = config('wechat.token'); $encodingAESKey = config('wechat.encodingAESKey'); $server = new Server($appId, $token); Log::info('写日志'); // 监听所有类型 $server->on('message', function ($message) { return Message::make('text')->content('您好!'); }); // 监听指定类型 // $server->on('message', 'text', function($message) { // return Message::make('text')->content('我们已经收到您发送的图片!'); // }); $result = $server->serve(); echo $result; }
public static function proccess(Wechat $wechat, $message) { $plugins = $wechat->plugins; if (!count($plugins)) { return Message::make('text')->content('功能尚未开启.'); } foreach ($plugins as $key => $plugin) { self::$fun[] = $plugin->name; } $list = self::plugins(); foreach ($list as $key => $plugin) { if (in_array($key, self::$fun)) { $data = ['name' => $key]; $s = new $plugin($wechat, $message, $key); if ($s->getResult()) { Event::fire('wechat.response', [$wechat, $message, $data], false); return $s->getResult(); } } } return Message::make('text')->content('维护中.'); }
public function process() { $response = Event::fire('wechat.chat.pre', [$this->wechat, $this->message], true); if ($response) { return $response; } $url = $this->url . $this->key . '&info=' . $this->message->Content; $url = $url . '&userid=' . $this->message->FromUserName; $client = new \GuzzleHttp\Client(); $ret = $client->get($url); $ret = json_decode($ret->getBody(), true); $response = Event::fire('wechat.chat.post', [$this->wechat, $this->message, ['data' => $ret]], true); if ($response) { return $response; } if (isset($ret['code']) && ($ret['code'] = 100000)) { $this->result = Message::make('text')->content($ret['text']); } if (is_null($ret)) { $this->result = Message::make('text')->content('接口调用异常.'); } }
public function process() { /** * 触发条件,请自行修改 */ if (mb_substr($this->message->Content, 0, 2) != '解梦') { return null; } $response = Event::fire('wechat.jie_meng.pre', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } $word = str_replace('解梦', '', $this->message->Content); $key = '1f071a80dd53409da4186846206fc76a'; $url = 'http://api.avatardata.cn/ZhouGongJieMeng/LookUp?key=' . $key . '&keyword=' . $word; $result = $this->getRequest($url); $response = Event::fire('wechat.jie_meng.post', [$this->wechat, $this->message], true); if ($response) { $this->result = $response; return true; } if (!isset($result['total']) || !$result['total']) { $this->result = Message::make('text')->content('周公不能解此梦.'); return true; } $data = array_flip(array_column($result['result'], 'title')); if (array_key_exists($word, $data)) { $index = $data[$word]; $text = str_replace(['<br/>', '<br>'], "\n", $result['result'][$index]['content']); $text = strip_tags($text); $this->result = Message::make('text')->content($text); return true; } $this->result = Message::make('text')->content('周公也无能为力.'); }
public function index() { //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8'); $appId = 'wx8f39f3bea52b4a70'; $token = "abwxwx"; $encodingAESKey = "xvK813j7NbymjgiAYCxtUCgAOlYWhdZ0ak0dxyLB89Q"; $server = new Server($appId, $token, $encodingAESKey); $server->on('event', 'subscribe', function ($event) { error_log('收到关注事件,关注者openid: ' . $event['FromUserName']); return Message::make('text')->content('感谢您关注'); }); $server->on('event', 'unsubscribe', function ($event) { error_log('取消关注事件,关注者openid: ' . $event['FromUserName']); }); $server->on('message', function ($message) { return "您好!欢迎关注!"; }); // 您可以直接echo 或者返回给框架 echo $server->serve(); //$signin = U('index/signin', [], true, true); //$jspath = __DIR__."../View/Public/js/app.js"; //$this->assign('signUrl', $signin)->assign('jsPath', $jspath); $this->display(); }
/** * 生成回复内容 * * @param mixed $response * * @return string */ protected function response($response) { if (empty($response)) { return ""; } is_string($response) && ($response = Message::make('text')->with('content', $response)); $return = ""; if ($response instanceof BaseMessage) { $response->from($this->input->get('ToUserName'))->to($this->input->get('FromUserName')); $this->call('responseCreated', array($response)); $return = $response->buildForReply(); if ($this->security) { $return = $this->getCrypt()->encryptMsg($return, $this->input->get('nonce'), $this->input->get('timestamp')); } } $return = $this->call('served', array($return), $return); return $return; }
<?php require __DIR__ . "/autoload.php"; // 路径请修改为你具体的实际路径 use Overtrue\Wechat\Server; use Overtrue\Wechat\Message; // $corpId = 'wx926454a4965d2b7c'; // $appId = '3'; $appId = 'wx926454a4965d2b7c'; $secret = 'PsPkG1V74sWm6V2mNN3F2NOHKQT696g1wrmJZhB7WT-8xcLmt-3rez3FfuH3SzCf'; $token = 'N6cq3R'; $encodingAESKey = 'nil3OzTxXoPHx4mvx0GLbyECkPYrfyiB9ca4Iz61ny3'; $server = new Server($appId, $token, $encodingAESKey); // 监听所有类型 $server->on('message', function ($message) { return Message::make('text')->content('您好!'); }); $result = $server->serve(); echo $result;
/** * 生成回复内容 * * @param mixed $response * * @return string */ protected function response($response) { is_string($response) && ($response = Message::make('text')->with('content', $response)); if ($response instanceof BaseMessage) { $response->from($this->input('ToUserName'))->to($this->input('FromUserName')); $xml = $response->buildForReply(); if ($this->security) { return $this->service('crypt')->encryptMsg($xml, $this->input('nonce'), $this->input('timestamp')); } return $xml; } return null; }
/** * 回复视频. * * @param App\Models\Material $video 素材 * * @return Response */ private function replyVideo($video) { return WechatMessage::make('video')->media($video->original_id)->title($video->title)->description($video->description); }
/** * 处理微信的请求消息 * * @param Server $server * * @return string */ public function anyApi() { $url = Config::get('app.url'); $server = new Server(option('wechat_app_id'), option('wechat_token'), option('wechat_encode_key')); //关注回复 $server->on('event', 'subscribe', function ($event) { return Message::make('text')->content('您好!欢迎关注玫瑰花开网'); }); /*$button = new MenuItem("公司介绍",'click', 'about'); $buttona = new MenuItem("活动专场",'click','activity'); $buttonb = new MenuItem("个人中心",'click','person'); $menus = array( $button->buttons(array( new MenuItem('关于公司', 'click', 'about'), new MenuItem('业务介绍', 'click', 'business'), new MenuItem('联系我们', 'click', 'contact'), new MenuItem('婚恋业务', 'click', 'marry'), )), $buttona->buttons(array( new MenuItem('待选嘉宾', 'click', 'guest'), new MenuItem('才俊专场', 'click', 'talent'), new MenuItem('成功案例', 'click', 'case'), new MenuItem('会员搜索', 'click', 'search'), )), $button, $buttona, $buttonb, ); try { //$menu = new Menu('wxd4a0943fa53c5435','22f260d0c5a4736ed7bb50558d52b720'); $menu = App::make('Overtrue\Wechat\Menu'); $menu->set($menus);// 请求微信服务器 echo '设置成功!'; } catch (\Exception $e) { echo '设置失败:' . $e->getMessage(); }*/ $server->on('event', 'click', function ($event) use($url) { switch ($event->EventKey) { case 'about': return Message::make('news')->items(function () use($url) { return array(Message::make('news_item')->title('关于公司')->description('玫瑰花开公司介绍')->url($url . '/article/1')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('业务介绍')->description('玫瑰花开业务介绍')->url($url . '/article/9')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('联系我们')->description('联系我们')->url($url . '/article/10')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('婚恋业务')->description('玫瑰花开婚恋业务')->url($url . '/article/11')->picUrl('http://www.baidu.com/demo.jpg')); }); break; case 'activity': return Message::make('news')->items(function () use($url) { return array(Message::make('news_item')->title('活动专场')->description('玫瑰花开活动专场')->url($url)->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('待选嘉宾')->description('玫瑰花开待选嘉宾')->url($url . '/article/12')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('才俊专场')->description('玫瑰花开才俊专场')->url($url . '/viplist_member')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('成功案例')->description('玫瑰花开业成功案例')->url($url . '/article/13')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('会员搜索')->description('玫瑰花开会员搜索')->url($url . '/search')->picUrl('http://www.baidu.com/demo.jpg')); }); break; case 'person': $openid = $event->FromUserName; return Message::make('news')->items(function () use($openid, $url) { return array(Message::make('news_item')->title('报名通道')->description('参与玫瑰花开报名')->url($url . '/weixin/register?openid=' . $openid)->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('绑定会员')->description('绑定会员')->url($url . '/weixin/login?openid=' . $openid)->picUrl('http://www.baidu.com/demo.jpg')); }); break; } }); return $server->serve(); // 或者 return $server; }
/** * 处理文字消息 * @param $text string * @param $openid string * @param $welcome closure * @return mixed */ function handleText($text, $openid, $welcome) { if (in_array(trim($text), array("你好", "你能干什么", "哈哈", "您好", "喂", "你有什么功能"))) { return Message::make('news')->items($welcome); } if (in_array(trim($text), array("四六级", "46", "查分"))) { $url = "5.n1gel.sinaapp.com/cet.php?openid={$openid}"; return Message::make('news')->item(Message::make('news_item')->title("四六级查分-先来备份一下考号吧~")->PicUrl("http://n1gel-n1gel.stor.sinaapp.com/cet_cover.jpg")->description("大家期待已久的四六级查分功能终于做好啦!!\n不过,考试成绩得两个月后才会公布,那么久考号早就丢了吧…\n快来备份一下考号吧,成绩公布后,我会第一时间通知你们哦,到时候还是回复『46』就可以直接查到分数啦!\n")->url($url)); } $url = "http://www.tuling123.com/openapi/api?"; $params = array("key" => "08ad04b298923b29a203d0aca21a9779", "info" => $text); $url .= http_build_query($params); //换成使用curl,哈哈,时间变成了1/3,太厉害啦! $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300); $response = curl_exec($ch); $data = json_decode($response, true); if ($data['code'] == 200000) { //处理链接类请求 $link = "<a href=\"" . $data['url'] . "\"> 『点击查看』</a>"; return Message::make('text')->content($data['text'] . $link); } elseif ($data['code'] == 100000) { //返回天气news $weatherArray = explode(';', $data['text']); //用;分,最后会有一个空字符串 if (count($weatherArray) == 5) { $city = strtok($weatherArray[0], ':'); //今日天气特殊处理 $items[0]['title'] = str_replace(',', "\n", strtok(':')); //取出天气状况,决定天气图标 $tmp = explode(' ', $items[0]['title']); $items[0]['weather'] = $tmp[3]; for ($i = 1; $i < 4; ++$i) { $items[$i]['title'] = str_replace(',', "\n", $weatherArray[$i]); $tmp = explode(' ', $items[$i]['title']); $items[$i]['weather'] = $tmp[2]; } //这是返回数据的BUG // $items[1]['title'] = str_replace('大雪', "~~ ", $items[1]['title']); foreach ($items as &$item) { if (strstr($item['weather'], "多云转晴")) { $item['url'] = "http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E6%99%B4%E8%BD%AC%E5%A4%9A%E4%BA%91.png"; } elseif (strstr($item['weather'], "阵雨转多云")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E9%9B%A8%E8%BD%AC%E5%A4%9A%E4%BA%91.png'; } elseif (strstr($item['weather'], "晴")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E6%99%B4.png'; } elseif (strstr($item['weather'], "多云")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E5%A4%9A%E4%BA%91.png'; } elseif (strstr($item['weather'], "小雪")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E9%9B%AA.png'; } elseif (strstr($item['weather'], "雨")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E5%A4%A7%E9%9B%A8.png'; } elseif (strstr($item['weather'], "阴")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E9%98%B4.png'; } elseif (strstr($item['weather'], "雪")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E5%A4%A7%E9%9B%AA.png'; } elseif (strstr($item['weather'], "小雨")) { $item['url'] = 'http://n1gel-n1gel.stor.sinaapp.com/weather%2F%E5%B0%8F%E9%9B%A8.png'; } else { $item['url'] = "http://n1gel-n1gel.stor.sinaapp.com/weather%2F528a43662164c_12.png"; } } return Message::make('news')->items(function () use($city, $items) { return array(Message::make('news_item')->title("亲,已为你找到{$city}的天气信息")->PicUrl("http://n1gel-n1gel.stor.sinaapp.com/weather%2Fweather_cover.jpg"), Message::make('news_item')->title($items[0]['title'])->PicUrl($items[0]['url']), Message::make('news_item')->title($items[1]['title'])->PicUrl($items[1]['url']), Message::make('news_item')->title($items[2]['title'])->PicUrl($items[2]['url']), Message::make('news_item')->title($items[3]['title'])->PicUrl($items[3]['url'])); }); } return Message::make('text')->content($data['text']); } return Message::make('text')->content($data['text']); }
public function serve(Server $server) { // 监听所有类型 $server->on('event', 'click', function ($event) { $appId = Config::get('wechat.app_id'); $appSecret = Config::get('wechat.secret'); $access_token = $this->access_token; $message = ''; $media = new media($appId, $appSecret); $arr = $media->lists('news', '0', '20'); if ($event['EventKey'] == 'a') { $media_id = '3gp3sK9--7rGxLoN_n78eYEDWwXJEB52EGa5QqvvAe8'; } else { if ($event['EventKey'] == 'b') { $media_id = 'dk6HHcmPn8H907So8ayA5UAd3K6i48AwZdMFE_5aV3I'; } } foreach ($arr['item'] as $v) { if ($v['media_id'] == $media_id) { foreach ($v['content']['news_item'] as $val) { $title = $val['title']; $digest = $val['digest']; $url = $val['url']; $imgid = $val['thumb_media_id']; $imgurl = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=' . $access_token; $array = array('type' => 'image', 'offset' => '0', 'count' => '20'); $array = json_encode($array); $response = Curl::post($imgurl, array(), $array); $arr = json_decode($response); foreach ($arr->item as $k) { if ($k->media_id == $imgid) { $imgurl = $k->url; } } $message[] = Message::make('news_item')->title($title)->description($digest)->url($url)->picUrl($imgurl); } } } $this->message = $message; if ($event['EventKey'] == 'a') { return Message::make('news')->items(function () { $mes = array(); foreach ($this->message as $v) { array_push($mes, $v); } return $mes; }); } else { if ($event['EventKey'] == 'b') { return Message::make('news')->items(function () { $mes = array(); foreach ($this->message as $v) { array_push($mes, $v); } return $mes; }); } } }); $server->on('message', function ($message) { return "欢迎关注 Skyware!"; }); $result = $server->serve(); echo $result; }
public function sendMessage($message, $openId) { $staff = new Staff($this->_appId, $this->_secret); $staff->send(Message::make('text')->content($message))->to($openId); return true; }
public function notification() { Log::write('php://input' . file_get_contents('php://input')); // 监听文体类型 $this->_server()->on('message', 'text', function ($message) { return AutoReplyHelper::autoReplyMessage($message); }); $this->_server()->on('event', 'subscribe', function ($event) { $welcome_guide = <<<EOD 你来啦亲爱的[害羞] 终于等到你,还好我没放弃~[玫瑰] 我们致力于为你打造一个一站式学习生活服务平台 0首付分期购[怄火],5分钟零食送货上床[害羞],兼职换购[奋斗],查课表[转圈],查成绩[得意],现在在指尖微校园公众号都可以直接实现![色] 你的每一次参与,都在见证我们的成长。 更多精彩福利,尽在指尖分期! 关注我们,指尖学长带你嗨翻大学生活![悠闲] 别忘了点击下方菜单栏按钮注册、绑定、下载APP加入我们哦![拥抱] (客服电话:4006118602 指尖客服妹纸期待您的骚扰~[爱心][玫瑰][害羞]) EOD; return Message::make('text')->content($welcome_guide); }); $this->_server()->on('event', 'CLICK', function ($event) { $openid = $event->FromUserName; $eventKey = $event->EventKey; $user = M('user_weixin')->where(['openid' => $openid])->find(); $new_item = null; if (is_null($user)) { $reg_url = make_weixin_url('/user/reg', ['openid' => $openid]); $login_url = make_weixin_url('/user/login', ['openid' => $openid]); $use_guide = <<<EOD 为了方便您的后续使用,你需要: 【已注册用户】<a href="{$login_url}">绑定指尖大学账号</a> 【未注册用户】<a href="{$reg_url}">注册指尖大学账号</a> EOD; return Message::make('text')->content($use_guide); } $token = $user['token']; $query = ['openid' => $openid, 'token' => $token, 'ref' => 'weixin']; if ($eventKey == 'CAMPUS') { $term_url = make_weixin_url('/user/termtable', $query); $week_url = make_weixin_url('/user/weektable', $query); $subject_url = make_weixin_url('/user/subjects', $query); $score_url = make_weixin_url('/user/scores', $query); $exam_url = make_weixin_url('/user/exams', $query); $campus_nav = <<<EOD 欢迎使用指尖微校园的,课表查询功能。 ** 注意:您需要先在我们的指尖大学App 导入过相应的课表、考试,成绩之后才可以在这里查询 ** 【1】<a href="{$week_url}">本周课表</a> 【2】<a href="{$term_url}">学期课表</a> 【3】<a href="{$subject_url}">课程列表</a> 【4】<a href="{$score_url}">我的成绩</a> 【5】<a href="{$exam_url}">我的考试</a> EOD; return Message::make('text')->content($campus_nav); } else { if (in_array($eventKey, ['REG', 'LOGIN'])) { $reg_url = make_weixin_url('/user/reg', $query); $login_url = make_weixin_url('/user/login', $query); $reg_bind_nav = <<<EOD 【1】<a href="{$reg_url}">尚未注册:注册请点我</a> 【2】<a href="{$login_url}">已经注册:绑定登录请点我</a> EOD; return Message::make('text')->content($reg_bind_nav); } else { if ($eventKey == 'DOWNLOAD') { $download_ios_url = "https://itunes.apple.com/app/apple-store/id912174294?pt=102023806&ct=zjwxy&mt=8"; $download_android_url = "http://www.yszjdx.com/?from=wx"; $download_nav = <<<EOD 如果链接下载不了, 苹果同学请移步 App Store 搜索“指尖大学”进行下载,安卓同学请在各应用商店中寻找“指尖大学”。 更多帅哥萌妹尽在指尖大学,等你哟![害羞] <a href="{$download_ios_url}"> →iOS 下载点我← </a> <a href="{$download_android_url}">→安卓下载点我← </a> EOD; return Message::make('text')->content($download_nav); } else { $base_loan_url = $this->_loan_url; $base_mobile_url = $this->_mobile_url; $event_url_map = ['my_order' => $base_loan_url . '/user/order', 'my_bill' => $base_loan_url . '/user/bill', 'fen_qi' => $base_loan_url . '/', 'discount' => $base_loan_url . '/list/index/search/限时', 'cash' => $base_loan_url . '/goods/index/id/39', 'check' => $base_mobile_url . '/credit', 'lottery' => $base_mobile_url . '/lottery/index/1', 'jobs' => $base_mobile_url . '/job/', 'jiaxiao' => $base_loan_url . '/list/index/search/驾校', 'shopping_day' => $base_loan_url . '/topic/electric_cars.html', 'zj_exchange' => $base_loan_url . '/topic/job.html', 'iphone6s' => $base_loan_url . '/list/index/search/apple%20iphone%206s']; $event_name_map = ['my_order' => '我的订单', 'my_bill' => '我的账单', 'fen_qi' => '指尖商城', 'discount' => '指尖特惠', 'cash' => '应急包', 'check' => '签到', 'lottery' => '指尖抽奖', 'jobs' => '指尖兼职', 'jiaxiao' => '指尖驾校', 'shopping_day' => '大学生购物节', 'iphone6s' => '6S 专区', 'zj_exchange' => '兼职换购']; $event_tip_map = []; $key = strtolower($eventKey); $base_url = $event_url_map[$key]; if (empty($base_url)) { return Message::make('text')->content('没有找到跳转链接'); } $tip_info = ''; if (isset($event_tip_map[$key])) { $tip_info = $event_tip_map[$key]; } $nav_name = $event_name_map[strtolower($key)]; $fenqi_url = $base_url . '?from=wx&token=' . $token; if (!empty($tip_info)) { $fenqi_nav = <<<EOD {$tip_info} <a href="{$fenqi_url}"> → {$nav_name} 点我跳转 ← </a> EOD; } else { $fenqi_nav = <<<EOD <a href="{$fenqi_url}"> → {$nav_name} 点我跳转 ← </a> EOD; } return Message::make('text')->content($fenqi_nav); } } } }); echo $this->_server()->serve(); }
/** * location类型消息解析, 可基于用户发送位置开发功能模块 * *@param Message $message 消息对象 * * @return Response */ private static function location($message) { return Message::make('text')->content('收到位置:' . $message->Label); }
/** * image类型消息解析, 可从微信服务器下载图片到本地 * *@param Message $message 消息对象 * * @return Response */ private static function image($message) { //Queue::push(new DownloadResource($message)); return Message::make('text')->content('收到图片, MediaId == ' . $message->MediaId); }
/** * link类型消息解析 * *@param Message $message 消息对象 * * @return Response */ private static function link($message) { return Message::make('text')->content('收到链接!链接== ' . $message->Url); }
/** * voice类型消息解析, 可获取微信服务器解析的语音文本内容自定义回复消息 * * @param Message $message 消息对象 * * @return Response */ private static function voice($message) { //Queue::push(new DownloadResource($message)); return Message::make('text')->content('收到语音消息!识别后内容 == ' . $message->Recognition); }