机器人接口使用的是openai?https://openai.com/
b2问答发布成功钩子:
add_action('b2_user_ask_post_success', 'xmw_ask_answer',20,2);
xmw_ask_answer函数:
//机器人回答 function xmw_ask_answer($user_id, $post_id){ $post_data = get_post($post_id);//获取文章信息 $post_title = $post_data->post_title;//文章标题 $content = xmw_ask_post($post_title); $arg = array( 'ID'=> '0', 'post_title'=>b2_get_des(0,60,$content), 'post_content' => wp_slash($content), 'post_status' => 'publish',//回答会公开 'post_author' => null,//回答者用户id 'post_parent'=>$post_id, 'post_type'=>'answer' ); $post_id = wp_insert_post($arg,true); }
xmw_ask_post请求函数:
//$data参数为问题 function xmw_ask_post($data){ $url = 'https://api.openai.com/v1/completions';//openai机器人接口地址 $token = '';//填写openai机器人的请求密钥Token $ask = array( 'model' => 'text-davinci-003',//模板 'prompt' => $data,//问题 'max_tokens' => 3000, ); $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array( 'content-type' => 'application/json', 'Authorization' => 'Bearer '.$token, ), 'body' => json_encode($ask), 'cookies' => array() ) ); $message = $response['body']; $s = json_decode($message, true);//将$message JSON强制转换为数组 return $s['choices'][0]['text'];//返回机器人的回答 }
将上方代码逐个放入主题的functions.php中。
openai的密钥需要自己去申请了