This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathwechat-robot.php
164 lines (140 loc) · 4.62 KB
/
wechat-robot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
Plugin Name: Wechat Robot
Plugin URI: http://github.com/wangvsa/wechat-robot
Description: Wechat Robot的功能包括:0.访问站点 1.查看最新文章 2.查看随机文章 3.查看热门文章 4.关键字搜索 5.访问微社区
Version: 1.0
Author: WangChen
Author URI: http://wangchen.info
*/
define('WEIXIN_ROBOT_PLUGIN_DIR', WP_PLUGIN_DIR.'/'. dirname(plugin_basename(__FILE__)));
require(WEIXIN_ROBOT_PLUGIN_DIR.'/wechat.php');
add_action('parse_request', 'wechat_robot_redirect', 4);
function wechat_robot_redirect( $wp ) {
if( isset( $_GET['wechat'] ) ) {
$robot = new WechatRobot("wechat", "wx0fc7ca6909bc89e0", "0a05f620788890fe452be1f443bf45bb", true);
$robot->create_menu('{
"button":[
{
"name":"阅读文章",
"sub_button":[
{
"type":"click",
"name":"最新文章",
"key":"MENU_RECENT_POSTS"
},
{
"type":"click",
"name":"随机文章",
"key":"MENU_RANDOM_POSTS"
},
{
"type":"click",
"name":"热门文章",
"key":"MENU_HOTEST_POSTS"
}
]
},
{
"type":"view",
"name":"FB主页",
"url":"http://www.freebuf.com"
},
{
"type":"view",
"name":"微社区",
"url":"http://wx.wsq.qq.com/187329269"
}]
}');
$robot->run();
}
}
class WechatRobot extends Wechat {
protected function queryAndResponse( $arg ) {
$the_query = new WP_Query( $arg );
if( $the_query->have_posts() ) {
$counter = 0;
$items = array();
while( $the_query->have_posts() ) {
$the_query->the_post();
global $post;
$title = get_the_title();
$link = get_permalink();
$excerpt = wechat_get_excerpt( get_the_excerpt() );
if ( $counter == 0 ) {
$thumb = wechat_get_thumb($post, array(640, 320));
} else {
$thumb = wechat_get_thumb($post, array(80, 80));
}
$new_item = new NewsResponseItem($title, $excerpt, $thumb, $link);
array_push($items, $new_item);
// 最多显示3篇
if ( ++$counter == 3 )
break;
}
$this->responseNews($items);
} else {
$this->responseText("不好意思~没有找到您想要的东东~请换个关键字再试试?");
}
wp_reset_postdata();
}
protected function searchPosts( $key ) {
$arg = array( 's' => $key );
$this->queryAndResponse( $arg );
}
protected function recentPosts() {
$arg = array( 'ignore_sticky_posts'=>1, 'showposts' => 3 );
$this->queryAndResponse( $arg );
}
protected function randomPosts() {
$arg = array ( 'ignore_sticky_posts' => 1, 'orderby' => 'rand' );
$this->queryAndResponse( $arg );
}
protected function hotestPosts() { // 本月最热门
$arg = array (
'ignore_sticky_posts' => 1,
'orderby' => 'comment_count',
'year' => date('Y'),
'monthnum' => date('m')
);
$this->queryAndResponse( $arg );
}
protected function onText() {
$msg = $this->getRequest('content');
if( $msg == '0' ) {
$this->responseText("访问FreeBuf黑客与极客,请点击下面连接:\n http://www.freebuf.com ");
} else if( $msg == '5' ) {
$this->responseText("访问FB社区,请点击下面连接:\n http://wx.wsq.qq.com/187329269 ");
} else if( $msg == '1' ) {
$this->recentPosts();
} else if ( $msg == '2' ) {
$this->randomPosts();
} else if ( $msg == '3' ) {
$this->hotestPosts();
} else if ( strncmp($msg, '4', 1)==0 ) { // starts with '4'
if( strlen($msg) == 1 ) {
$this->responseText("您没有输入关键字,要输入[4关键字]进行搜索哦,比如 4黑客 ");
} else {
$this->searchPosts( substr($msg, 1, strlen($msg)-1) );
}
} else {
$this->responseText("欢迎关注FreeBuf黑客与极客\n 回复[0]访问FreeBuf\n 回复[1]查看最新文章".
"\n 回复[2]查看随机文章\n 回复[3]查看热门文章\n 回复[4关键字]搜索文章\n 回复[5]访问FB社区\n 回复其他内容查看本菜单");
}
}
protected function onSubscribe() {
$this->responseText("欢迎关注FreeBuf黑客与极客\n 回复[0]访问FreeBuf\n 回复[1]查看最新文章".
"\n 回复[2]查看随机文章\n 回复[3]查看热门文章\n 回复[4关键字]搜索文章\n 回复[5]访问FB社区 回复其他内容查看本菜单");
}
// 点击菜单
protected function onClick() {
$key = $this->getRequest('EventKey');
if($key=="MENU_RECENT_POSTS") {
$this->recentPosts();
} else if($key=="MENU_RANDOM_POSTS") {
$this->randomPosts();
} else if($key=="MENU_HOTEST_POSTS") {
$this->hotestPosts();
}
}
}