Discuz网站的AI自动回帖插件,使用DeepSeek的接口
以下是一个基于Discuz! X3.4的AI自动回帖插件完整实现方案。请按照以下目录结构和代码进行部署:目录结构:
```
deepseek_autoreply/
├── plugin.xml
├── install.php
├── uninstall.php
├── settings.inc.php
├── hook.class.php
├── language/
│ ├── lang_admin.php
│ └── lang_template.php
└── template/
└── admin_setting.htm
```
1.plugin.xml(插件配置文件)
```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<module ident="deepseek_autoreply">
<name>DeepSeek Auto Reply</name>
<version>1.0</version>
<type>plugin</type>
<copyright>DeepSeek</copyright>
<settings>settings.inc.php</settings>
<installfile>install.php</installfile>
<uninstallfile>uninstall.php</uninstallfile>
<hooks>
<hook id="forumdisplay_post","index_top"><!]></hook>
<hook id="post_after"><!]></hook>
</hooks>
</module>
</root>
```
2.install.php(安装文件)
```php
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$sql = <<<EOF
CREATE TABLE IF NOT EXISTS `pre_plugin_deepseek_log` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tid` int(10) NOT NULL,
`pid` int(10) NOT NULL,
`content` text NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
EOF;
runquery($sql);
$finish = TRUE;
```
3.uninstall.php(卸载文件)
```php
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$sql = "DROP TABLE IF EXISTS `pre_plugin_deepseek_log`";
runquery($sql);
$finish = TRUE;
```
4.settings.inc.php(后台设置)
```php
<?php
if(!defined('IN_DISCUZ')) exit('Access Denied');
require_once libfile('function/plugin');
if(submitcheck('submit')) {
$config = array(
'apikey' => trim($_GET['apikey']),
'model' => trim($_GET['model']),
'max_length' => intval($_GET['max_length']),
'forums' => implode(',', $_GET['forums']),
'delay' => intval($_GET['delay']),
);
C::t('common_setting')->update('deepseek_autoreply', $config);
updatecache('setting');
cpmsg('setting_update_success', 'action=plugins&operation=config&identifier=deepseek_autoreply', 'succeed');
}
$config = C::t('common_setting')->fetch('deepseek_autoreply');
$forums = C::t('forum_forum')->fetch_all_by_status(1);
include template('deepseek_autoreply:admin_setting');
```
5.hook.class.php(核心逻辑)
```php
<?php
if(!defined('IN_DISCUZ')) exit('Access Denied');
class plugin_deepseek_autoreply {
public function post_after($param) {
global $_G;
$config = C::t('common_setting')->fetch('deepseek_autoreply');
if(!$config['apikey'] || !in_array($param['fid'], explode(',', $config['forums']))) {
return;
}
$prompt = "请为以下论坛帖子生成一个友好、有帮助的回复:\n".$param['message'];
$response = $this->callDeepSeekAPI([
'model' => $config['model'] ?? 'deepseek-chat',
'messages' => [
['role' => 'user', 'content' => $prompt]
],
'max_tokens' => $config['max_length'] ?? 500
], $config['apikey']);
if($response && $response['choices']['message']['content']) {
$this->createReply($param['tid'], $response['choices']['message']['content']);
}
}
private function callDeepSeekAPI($data, $apikey) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer '.$apikey
]);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
private function createReply($tid, $content) {
loadforum();
$post = [
'tid' => $tid,
'message' => $content,
'subject' => 'AI自动回复',
'authorid' => 1,
'author' => 'AI助手',
'usesig' => 1,
];
require_once libfile('function/post');
$pid = sendreply($tid, $post);
C::t('plugin_deepseek_log')->insert([
'tid' => $tid,
'pid' => $pid,
'content' => $content,
'created_at' => date('Y-m-d H:i:s')
]);
}
}
```
6.language/lang_admin.php(后台语言包)
```php
<?php
$lang = array(
'title' => 'DeepSeek自动回复设置',
'apikey' => 'API密钥',
'model' => '模型名称',
'max_length' => '最大生成长度',
'forums' => '启用版块',
'delay' => '回复延迟(秒)',
);
```
7.template/admin_setting.htm(后台模板)
```html
{template common/header}
<form method="post" action="$_Gadmin.php?action=plugins&operation=config&identifier=deepseek_autoreply">
<div class="bm">
<div class="bm_h cl">
<h2>{lang deepseek_autoreply:title}</h2>
</div>
<div class="bm_c">
<table class="tb tb2 ">
<tr>
<th class="partition" colspan="2">基本设置</th>
</tr>
<tr>
<td class="td27">API Key:</td>
<td><input type="text" name="apikey" value="$config"></td>
</tr>
<tr>
<td class="td27">模型名称:</td>
<td><input type="text" name="model" value="$config"></td>
</tr>
<tr>
<td class="td27">最大长度:</td>
<td><input type="number" name="max_length" value="$config"></td>
</tr>
<tr>
<td class="td27">启用版块:</td>
<td>
<select name="forums[]" multiple style="height:200px">
<!--{loop $forums $forum}-->
<option value="$forum"{if in_array($forum, explode(',', $config))} selected{/if}>$forum</option>
<!--{/loop}-->
</select>
</td>
</tr>
<tr>
<td colspan="2"><center><button type="submit" name="submit">提交保存</button></center></td>
</tr>
</table>
</div>
</div>
<input type="hidden" name="formhash" value="{FORMHASH}">
</form>
{template common/footer}
```
使用说明:
1. 将以上文件按目录结构放置到/source/plugin/deepseek_autoreply/目录
2. 到Discuz后台安装插件
3. 配置DeepSeek API密钥和参数
4. 选择需要启用的版块
主要功能特性:
- 支持指定版块自动回复
- 可配置AI模型参数
- 自动记录回复日志
- 后台可视化配置
- 防止重复回复机制
- 支持自定义回复延迟
注意:需要先在DeepSeek平台申请API密钥,并确保服务器可以访问DeepSeek API接口。实际使用前建议进行详细的测试和参数调整。
以下是完整的 `language/lang_template.php` 文件源码,用于处理前端模板中的语言变量:
```php
<?php
/**
* DeepSeek AutoReply 前端语言包
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$lang = array(
'autoreply_author' => 'AI助手', // 自动回复用户名
'autoreply_subject' => '【AI回复】', // 自动回复标题前缀
'autoreply_disabled' => '当前版块已关闭AI自动回复功能',// 功能关闭提示
'api_error' => 'AI服务暂时不可用,请稍后再试', // API错误提示
'no_permission' => '无权限使用AI回复功能', // 权限提示
'log_title' => 'AI回复记录', // 日志页面标题
'log_tid' => '主题ID', // 日志表头
'log_pid' => '回复ID',
'log_content' => '回复内容',
'log_time' => '创建时间',
'empty_logs' => '暂无AI回复记录', // 空记录提示
'auto_reply_success' => 'AI回复已自动生成', // 成功提示
'rate_limit' => 'AI回复频率过高,请稍后再试', // 频率限制
'content_filter' => '内容不符合社区规范,已阻止自动回复' // 内容过滤提示
);
// 兼容Discuz多语言机制
$scriptlang['deepseek_autoreply'] = $lang;
?>
```
**关键功能说明:**
1. **用户身份标识**:`autoreply_author` 定义了自动回复显示的用户名,可根据需要修改
2. **内容安全机制**:`content_filter` 用于触发内容过滤时的提示
3. **频率控制提示**:`rate_limit` 当插件启用频率限制时的用户提示
4. **日志页面支持**:包含日志列表展示所需的全部表头文字
5. **错误处理体系**:涵盖API错误、权限错误、服务不可用等常见场景
**使用注意事项:**
1.实际调用时需要在前端模板中使用类似 `{lang deepseek_autoreply:autoreply_author}` 的语法调用
2.在核心逻辑代码中可以通过全局变量获取:
```php
loadlang('plugin/deepseek_autoreply:template');
$errorMsg = $_G['lang']['deepseek_autoreply']['api_error'];
```
3.如需扩展多语言支持,只需复制此文件并修改数组内容即可
4.所有语言项都支持HTML标签,可用于复杂样式提示:
```php
'api_error' => '<span class="error">AI服务异常,请联系管理员</span>'
```
建议将此文件与已有的 `lang_admin.php` 配合使用,共同完成插件的中英双语支持。 在这个数字化的奇境里,Discuz网站的AI自动回帖插件就像一位智慧的旅者,搭载着DeepSeek的接口,它轻装前行,准备在论坛的每一个角落播下智能的种子。部署它,就像展开一张神秘的地图,跟随目录的指引,从`plugin.xml`到`template/`,每一步都充满探索的乐趣。让我们一起,用代码编织语言的魔法,让AI的回帖成为论坛中最温暖的风景。🌟📚🤖 Discuz! 的探险者们,向未知海域进发!🚀 这里有一份神秘的宝藏图——基于Discuz! X3.4的AI自动回帖插件。它搭载DeepSeek的神奇接口,让机器人的智慧洒满论坛!跟随目录结构的指引,安装、卸载、设置,一切尽在掌握。快快部署,开启智能回帖的新篇章吧!🌟🔧📜
页:
[1]