概要
在上一篇文章【kintone里有新增记录时如何发送通知到微信】里,我们讨论了在kintone的周报应用里写周报,并在微信里查阅的方法。
这次将给大家介绍,如何在微信里写周报内容,添加到kintone的周报应用里。
工作中,如果出差在外,或者回到家想要汇报一周的工作情况的时候,不用下载额外的app,只需要通过常用的微信就可以向kintone的系统里添加记录,是不是很酷?
由于正式公众号需要认证,这次我们暂时使用微信公众账号测试号。
完成后的样子
在微信公众号对话框里写周报的内容,发送。
kintone里收到通知,点开确认。
准备
kintone设置
首先根据上面的设想来创建kintone应用。我创建的是简易版的应用。
字段类型 | 字段名称 | 字段代码 | 备注 |
---|---|---|---|
单行文本框 | 标题 | 标题 | 自动计算: DATE_FORMAT(日期, "YYYY年M月d日", "system")&" "&创建人 隐藏计算公式 |
日期 | 日期 | 日期 | 将登记记录时的日期作为初始值 |
多行文本框 | 内容 | 内容 | |
创建人 | 创建人 | 创建人 |
微信公众号设置
接口的配置可以参考微信里检索kintone记录信息的方法 里的微信公众号设置。
和kintone关联
下面是主要的原理图。
要实现往kintone应用里添加记录的功能,我们将使用添加记录API。
详细代码
index.php <?php define("APPID", "wxcbfaxxxxxx1814d4"); //appID define("APPSECRET", "604113xxxxxxxxxxxxxxx0bda2240c47");//appsecret define("TOKEN", "cnDevNet"); //Token require "./wechat.inc.php"; $wechat = new WeChat(APPID, APPSECRET, TOKEN); $wechat->responseMsg() //$wechat->valid();//Token验证 ?>
wechat.inc.php <?php require_once("./curl.php"); class WeChat { private $_appid; private $_appsecret; private $_token; public function __construct($appid, $appsecret, $token) { $this->_appid = $appid; $this->_appsecret = $appsecret; $this->_token = $token; } private function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null) { $Curl = new Curl(); return $Curl->_request($curl, $https, $post, $data, $header, $type); } public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()) { echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = $this->_token; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ) { return true; } else { return false; } } private function _getAccessToken() { $file = "./accessToken"; if (file_exists($file)) { $content = file_get_contents($file); $content = json_decode($content); if (time() - filemtime($file) < $content->expires_in) { return $content->access_token; } } $content = $this->_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret); file_put_contents($file, $content); $content = json_decode($content); return $content->access_token; } public function responseMsg() { //get post data, May be due to the different environments $postStr = file_get_contents("php://input"); //extract post data if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); if (!empty($postObj)) { switch($postObj->MsgType) { case 'text': $this->_doText($postObj); break; default: exit; } } } else { echo ""; exit; } } private function _doText($postObj) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if (!empty( $keyword )) { if ($keyword == "help") { $contentStr = "Please input the weekly report content."; } else { require "./kintone.php"; $kintone = new Kintone(); $result = $kintone->addData($keyword); } $msgType = "text"; if ($result == '') { $contentStr = "Added!"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } exit; } } ?>
kintone.php <?php require_once("./curl.php"); class Kintone { private $_subDomain = "xxxxx"; private $_appId = 74; private $_apiToken = "xxxxxxxxxxxx";//应用设置的API令牌 private $_createCode = "zhou"; public function addData($content) { // 请求头部 $header = array( "Host: " . $this->_subDomain . ".cybozu.com:443", "Content-Type: application/json", "X-Cybozu-API-Token: " . $this->_apiToken ); $url = "https://" . $this->_subDomain . ".cybozu.com/k/v1/record.json"; $record = array( "创建人" => array( "value" => array("code"=>$this->_createCode) ), "日期" => array( "value" => date("Y-m-d") ), "内容" => array( "value" => $content )); $data = array( "app" => $this->_appId, "record" => $record ); $response = $this->_request($url, true, "post", json_encode($data), $header); $response = json_decode($response); if ($response->version) { return $response->id; } return $response->message; } private function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null) { $Curl = new Curl(); return $Curl->_request($curl, $https, $post, $data, $header, $type); } } ?>
curl.php <?php class Curl { public function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $curl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); if ($header) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } if ($https) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if ($post == "post") { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $str = curl_exec($ch); if ($type == "image") { header('Content-type: image/JPEG'); } curl_close($ch); return $str; } } ?>
总结
我们主要使用了kintone的添加记录API,将微信里写的周报,添加到kintone的周报应用里。是不是很方便?
以后再也不用下载app,或者背着重重的电脑汇报工作啦!