Update Wechat supported

Update Wechat supported
This commit is contained in:
Denny Dai 2015-10-31 20:33:17 +08:00
parent f3d1d90b43
commit 4fdd1535c9
4 changed files with 62 additions and 25 deletions

48
BOT.php
View File

@ -2,36 +2,36 @@
//init
require_once('config.php');
require_once('functions.php');
$plugins = [];
$plugins = array();
require(PATH.'classes'.DIRECTORY_SEPARATOR.TYPE.DIRECTORY_SEPARATOR.'Base.php');
require_once(PATH.'classes'.DIRECTORY_SEPARATOR.TYPE.DIRECTORY_SEPARATOR.'init.php');
//load plugins
$load_plugins = glob(PATH.'plugins'.DIRECTORY_SEPARATOR.'*.php');
foreach ($load_plugins as $key => $value) {
if ($value == PATH.'plugins'.DIRECTORY_SEPARATOR.'help.php') {
unset($load_plugins[$key]);
}
}
foreach ($TAGs as $TAG) {
foreach ($load_plugins as $value) {
$plugin_name = substr($value, strlen(PATH.'plugins'.DIRECTORY_SEPARATOR), -4);
if (preg_match("/^(".preg_quote($TAG, '/').$plugin_name."|".preg_quote($TAG, '/').$plugin_name.preg_quote('@').BOT_NAME.") (.*)$/", $text, $matches)) {
$plugin_text = $matches[2];
$plugin_sendto = $from;
require_once $value;
break;
}elseif (preg_match("/^(".preg_quote($TAG, '/').$plugin_name.")|(".preg_quote($TAG, '/').$plugin_name.preg_quote('@').BOT_NAME.")$/", $text)) {
$plugin_text = $matches[2];
$plugin_sendto = $from;
require_once $value;
break;
}
}
}
require_once PATH.'plugins'.DIRECTORY_SEPARATOR.'help.php';
$load_plugins = glob(PATH.'plugins'.DIRECTORY_SEPARATOR.'*.php');
foreach ($load_plugins as $key => $value) {
if ($value == PATH.'plugins'.DIRECTORY_SEPARATOR.'help.php') {
unset($load_plugins[$key]);
}
}
foreach ($TAGs as $TAG) {
foreach ($load_plugins as $value) {
$plugin_name = substr($value, strlen(PATH.'plugins'.DIRECTORY_SEPARATOR), -4);
if (preg_match("/^(".preg_quote($TAG, '/').$plugin_name."|".preg_quote($TAG, '/').$plugin_name.preg_quote('@').BOT_NAME.") (.*)$/", $text, $matches)) {
$plugin_text = $matches[2];
$plugin_sendto = $from;
require_once $value;
break;
}elseif (preg_match("/^(".preg_quote($TAG, '/').$plugin_name.")|(".preg_quote($TAG, '/').$plugin_name.preg_quote('@').BOT_NAME.")$/", $text)) {
$plugin_text = $matches[2];
$plugin_sendto = $from;
require_once $value;
break;
}
}
}
require_once PATH.'plugins'.DIRECTORY_SEPARATOR.'help.php';

20
classes/Wechat/Base.php Normal file
View File

@ -0,0 +1,20 @@
<?php
class Base
{
public function exec($raw)
{
echo $raw;
return true;
}
public function msg($id, $msg)
{
return $this->exec("<xml>
<ToUserName><![CDATA[".$id."]]></ToUserName>
<FromUserName><![CDATA[".WC_UserName."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$msg."]]></Content>
<MsgId>".rand(1,9999999999999)."</MsgId>
</xml>");
}
}

14
classes/Wechat/init.php Normal file
View File

@ -0,0 +1,14 @@
<?php
$BOT = new Base;
$GET = $raw = file_get_contents("php://input");
//reply to...
preg_match('/<ToUserName><!\[CDATA\[(.*)\]\]><\/ToUserName>/', $raw, $FromUserName);
$FromUserName = $FromUserName[1];
preg_match('/<FromUserName><!\[CDATA\[(.*)\]\]><\/FromUserName>/', $raw, $ToUserName);
$ToUserName = $ToUserName[1];
preg_match('/<Content><!\[CDATA\[(.*)\]\]><\/Content>/', $raw, $Content);
$Content = $Content[1];
$from = $ToUserName;
$text = $Content;

View File

@ -13,4 +13,7 @@ define('TB_TOKEN', 'YOU_TELEGRAM_BOT_TOKEN_HERE');
define('TB_API_URL', 'https://api.telegram.org/bot'.TB_TOKEN.'/');
//For plugins
define('PLUGIN_GITHUB_TOKEN', 'YOU_GITHUB_TOKEN_HERE');
define('PLUGIN_GITHUB_TOKEN', 'YOU_GITHUB_TOKEN_HERE');
//For Wechat
define('WC_UserName', 'YOU_WECHAT_USERNAME_HERE');