c++ tuling123_使用图灵机器人api搭建微信聊天机器人php实现

本文介绍了如何结合微信PC版辅助工具和图灵机器人API创建微信聊天机器人。首先,通过hook技术实现微信消息发送,然后申请图灵机器人API并获取密钥,接着用PHP编写代码调用API并处理返回的聊天响应。最后,部署PHP脚本到服务器,并配置微信助手以使用该接口进行交互。
摘要由CSDN通过智能技术生成

之前通过hook技术实现了微信pc端发送消息功能,如果在结合图灵机器人就能实现微信聊天机器人。

逻辑如下:

下面我简单介绍一下步骤。首先,你需要下载我的微信助手,下载地址请参考我的博客文章:

通过对微信pc hook实现微信助手。

申请图灵机器人,并认证。申请地址,使用api接入并获取apikey(详细请参考图灵机器人官网) 。

使用php实现访问图灵机器人api。

php实现代码如下:<?php

class Tuling123

{

private $apiKey;

private $secret;

private $text;

private $userId = 1;

private $selfInfo = '';

public function __construct($apiKey, $userId, $selfInfo){

$this->apikey = $apiKey;

$this->secret = $secret;

$this->userId = $userId;

$this->selfInfo = $selfInfo;

}

public function tuling($text, $raw = false){

$this->text = $text;

$param = [

'perception' => [

'inputText' => [

'text' => $this->text,

],

'selfInfo' => $this->selfInfo

],

'userInfo' => [

'apiKey' => $this->apikey,

'userId' => $this->userId,

]

];

$result = json_decode('['.$this->post('http://openapi.tuling123.com/openapi/api/v2',json_encode($param)).']',true);

return $raw ? $result : $result[0]['results'][0]['values']['text'];

}

private function post($url,$data){

$curl = curl_init();

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 500);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($curl, CURLOPT_URL, $url);

$result = curl_exec($curl);

curl_close($curl);

return $result;

}

}

?>

TuLing.php

这段代码主要是封装图灵机器人api<?php

require __DIR__.'/TuLing.php';

/**

* 获取 post 参数; 在 content_type 为 application/json 时,自动解析 json

* @return array

*/

function initPostData()

{

if (empty($_POST)) {

$content = file_get_contents('php://input');

$post = (array)json_decode($content, true);

} else {

$post = $_POST;

}

return $post;

}

$selfInfo = [

'location' => [

'city' => '广州'

]

];

header('Content-Type:application/json');

$post = initPostData();

$userid=$post['wxid'];

$content=$post['content'];

//str_replace("_","","$userid") 将去除"_"的微信id作为图灵机器人的用户id,因为图灵机器人用户id不能含"_"

$data = new Tuling123('您的图灵机器人apikey',str_replace("_","","$userid"),$selfInfo);

$result = $data->tuling($content);

$json['wxid'] = $userid;

$json['content'] = $result;

echo json_encode($json,JSON_UNESCAPED_UNICODE);

?>

wechatrobot.php

之前将这两个文件发布服务器。发布之后的wechatrobot.php文件访问地址即使微信助手接口地址。

如我的发布后地址是:http://blog.yshizi.cn/wechatrobot.php

微信助手配置如下:

然后就可撩机器人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值