微赞api.php,文字回复模块

# 1.模块目录

![](https://box.kancloud.cn/d7bdcb1da00dede47740893362944e54_218x211.png)

# 2.消息处理流程

Api.php $engine = new WeEngine();$engine->start();

$message = $this->account->parse($postStr);

WeUtility::logging('trace', $message);

输出结果:

2016-06-07 14:27:25 trace :

------------

Array:

from : fromUser ;

to : toUser ;

time : 1465280845 ;

type : event ;

event : subscribe ;

tousername : toUser ;

fromusername : fromUser ;

createtime : 1465280845 ;

msgtype : event ;

eventkey : ;

msgid : 1234567890123456 ;

$pars = $this->analyze($message); private function analyze(&$message)

1)in_array($message['type'], array('event', 'qr')(事件或者扫码消息)call_user_func_array(array($this, 'analyze' . $message['type']), array(&$message));

2)method_exists($this, 'analyze' . $message['type']) (默认调取analyze+消息类型的方法)

$response = $this->process($par); private function process($param)

$processor = WeUtility::createModuleProcessor($param['module']);

(account.class.php里的createModuleProcessor()根据module名称查询)

public static function createModuleProcessor($name)

//拼装模块处理类名

$classname = "{$name}ModuleProcessor";

1)if(!class_exists($classname)) { 不存在模块的类,直接加载插件里面的processor.php中的类声明

$file = IA_ROOT . "/addons/{$name}/processor.php"

if(!is_file($file)) {

$file = IA_ROOT . "/framework/builtin/{$name}/processor.php";

}

2)$o = new $classname();

load()->model('module');

$o->module = module_fetch($name);//module.mod.php module_fetch() 获取当前公号下安装好的指定模块及模块信息

$o->__define = $file;

self::defineConst($o);

if($o instanceof WeModule) {

return $o;

}

$response = $processor->respond(); //执行具体的模块的processor.处理逻辑

例如CoverModuleProcessor类的处理逻辑

class CoverModuleProcessor extends WeModuleProcessor {

public function respond() {

global $_W;

$content = $this->message['content'];

//查询回复表中的id对应的回复内容

$reply = pdo_fetch('SELECT * FROM ' . tablename('cover_reply') . ' WHERE `rid`=:rid', array(':rid' => $this->rule));

if(!empty($reply)) {

load()->model('module');

$module = module_fetch($reply['module']);

if (empty($module) && !in_array($reply['module'], array('site', 'mc', 'card'))) {

return '';

}

$url = $reply['url'];

if(empty($reply['url'])) {

$entry = pdo_fetch("SELECT eid FROM ".tablename('modules_bindings')." WHERE module = :module AND do = :do", array(':module' => $reply['module'], ':do' => $reply['do']));

$url = url('entry', array('eid' => $entry['eid']));

}

if (!strexists($url, '&j=') && !empty($_W['acid'])) {

$url = str_replace("?i={$_W['uniacid']}&", "?i={$_W['uniacid']}&j={$_W['acid']}&", $url);

}

$news = array();

$news[] = array(

'title' => $reply['title'],

'description' => $reply['description'],

'picurl' => $reply['thumb'],

'url' => $url

);

return $this->respNews($news);

}

return '';

}

if($this->isValidResponse($response)) {

$hitParam = $par;

if(!empty($par['keyword'])) {

$hitKeyword = $par['keyword'];

}

break;

}

WeUtility::logging('params', $hitParam);

2016-06-07 14:27:25 params :

------------

Array:

message : Array ;

module : cover ;

rule : 32 ;

priority : 0 ;

keyword : Array ;

WeUtility::logging('response', $response);

2016-06-07 14:27:25 response :

------------

Array:

FromUserName : toUser ;

ToUserName : fromUser ;

MsgType : news ;

ArticleCount : 1 ;

Articles : Array ;

$resp = $this->account->response($response);

$resp = $this->clip($resp, $hitParam);

if(!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {

$resp = $this->account->encryptMsg($resp);

$resp = $this->account->xmlDetract($resp);

}

echo $resp;

$this->receive($hitParam, $hitKeyword, $response);

private function receive($par, $keyword, $response) {

if (in_array($this->message['event'], array('subscribe', 'unsubscribe')) || in_array($this->message['type'], array('subscribe', 'unsubscribe'))) {

if (in_array($this->message['event'], array('subscribe', 'unsubscribe')) || in_array($this->message['type'], array('subscribe', 'unsubscribe'))) {

$modules = uni_modules();

$core = array();

$core['name'] = 'core';

$core['subscribes'] = array('core');

array_unshift($modules, $core);

foreach($modules as $m) {

if(!empty($m['subscribes'])) {

if ($m['name'] == 'core' || in_array($this->message['type'], $m['subscribes'])

|| in_array($this->message['event'], $m['subscribes'])) {

$obj = WeUtility::createModuleReceiver($m['name']);

$obj->message = $this->message;

$obj->params = $par;

$obj->response = $response;

$obj->keyword = $keyword;

$obj->module = $m;

$obj->uniacid = $_W['uniacid'];

$obj->acid = $_W['acid'];

if(method_exists($obj, 'receive')) {

@$obj->receive();

}

}

}

}

} else {

$row = array();

$row['uniacid'] = $_W['uniacid'];

$row['acid'] = $_W['acid'];

$row['dateline'] = $par['message']['time'];

$row['message'] = iserializer($par['message']);

$row['keyword'] = iserializer($keyword);

unset($par['message']);

unset($par['keyword']);

$row['params'] = iserializer($par);

$row['response'] = iserializer($response);

$row['module'] = $par['module'];

$row['type'] = 1;

pdo_insert('core_queue', $row);

}

}

数据表的查询:

Ims_Rule(关键词回复规则表),加载module+processor.php文件时,查询的就是IMS_Rule这个表

# 3.视图页

display.html

# 4.文本

消息类型同公众平台官方不同之处在于将 event 类型拆分开为独立的消息类型, 避免了重复判断.

根据消息类型不同, 消息对象结构还存在不同的附加数据,按照类型定义如下:

文本消息

粉丝用户向公众号发送了一条普通文本消息(包括包含表情的消息, 或者纯表情消息)

处理文本消息可以实现简单的文本对话, 结合使用文本上下文(请参阅上下文处理)可以实现调查, 测试等复杂的交互.

$text_message = array(

// 全局数据

'tousername' => 'toUser'

'fromusername' => 'fromUser'

'createtime' => '123456789'

'msgtype' => 'text' // string: 消息类型

'content' => // string: 文本消息内容

'redirection' => false, // bool: 是否是重定向

'source' => null // string: 消息来源, 消息二次分析(目前来源:qr,click, 将扫码等事件转换为 text 事件.)

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微赞WZ微信公众平台开源程序-是微动力程序的衍生强化版,实现多微站,多客户转接,多功能群发,粉丝会员积分统一,模块嫁接等功能,具有高度的安全特性!100%开源,程序可自由控制!便于二次开发的开源微信公众平台管理系统!多用户微信公众平台管理系统可简单的实现管理员与各个用户间的授权与管理!已强化了功能模块的管理与应用,方便微信功能独立开发。 选择微赞的几个理由 微赞侧重的是功能实用性和后期运营的扩展性 1、微赞程序的承载能力强,反应速度快,远胜小猪,微信管家; 2、微赞程序内置大量DIY设计扩展属性,个性修改非常方便,这些小猪,微信管家都是不具备的; 3、微赞程序模板,模块都是可以独立安装拆卸的,非常利于后期的扩展需求,这些小猪,微信管家都是很欠缺的; 4、微赞程序新模块、新模板的开发非常简易,一切安装流水线自动生成,这些小猪,微信管家都是不具备的; 5、微赞程序功能很强大,而且覆盖面光,功能数量齐全,远胜小猪,微信管家; 6、程序内置多微站体系,一个公众号可以建立多个微站,这样栏目也可以用微站化了; 7、程序内置多客服转接,完美解决在线客服问题; 8、程序内置多功能群发,完美解决图片,文章,视频等信息的群发; 9、程序内置粉丝,会员体系,完美解决粉丝同步,会员积分,余额统一的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值