### [7b2问答机器人](https://wws.ysu2.com/article/80141) **Published:** 2022-12-26T03:16:47 **Author:** gao19970828 **Excerpt:** 7b2问答机器人 机器人接口使用的是openai https://openai.com/ b2问答发布成功钩子: add_action(‘b2_user_ask_post_success’, ‘xmw_ask_answer’,20,2); ### 7b2问答机器人 机器人接口使用的是[openai](https://www.ahap.cn/goto/aHR0cHM6Ly9vcGVuYWkuY29tLw==) https://openai.com/ ![](https://www.ysu2.com/wp-content/uploads/2022/12/2022122603163131.png) 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的密钥: \[content\_hide\]  机器人的请求密钥Token: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  \[/content\_hide\] **Tags:** 7b2, app, ps, 主题, 机器人, 模板, 转换 **Categories:** 精选插画 ---