feat(im): add WeChat integration with QR code login and long-polling support

- Extended the IMChannel interface to include WeChat as a platform, supporting long-polling mode and full output mode.
- Implemented WeChat QR code login functionality, allowing users to authenticate via a QR code.
- Added new API endpoints for generating and polling the status of WeChat QR codes.
- Updated the frontend to handle WeChat-specific UI elements and interactions, including dynamic form adjustments based on the selected platform.
- Enhanced localization files to include WeChat-related translations.
- Introduced a WeChat adapter for message handling and file downloading via the iLink Bot API.
This commit is contained in:
wizardchen
2026-03-23 18:27:21 +08:00
committed by lyingbug
parent 45ad36845e
commit d1c7da97f9
16 changed files with 1424 additions and 65 deletions
+26 -2
View File
@@ -183,10 +183,10 @@ export interface IMChannel {
id: string;
tenant_id?: number;
agent_id: string;
platform: 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost';
platform: 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost' | 'wechat';
name: string;
enabled: boolean;
mode: 'webhook' | 'websocket';
mode: 'webhook' | 'websocket' | 'longpoll';
output_mode: 'stream' | 'full';
session_mode?: 'user' | 'thread';
knowledge_base_id?: string;
@@ -237,3 +237,27 @@ export function getSuggestedQuestions(
const qs = query.toString();
return get<{ data: { questions: SuggestedQuestion[] } }>(`/api/v1/agents/${agentId}/suggested-questions${qs ? '?' + qs : ''}`);
}
// ===== WeChat QR Code Login =====
export interface WeChatQRCodeResult {
qrcode_url: string;
qrcode: string;
}
export interface WeChatQRCodeStatus {
status: 'wait' | 'scaned' | 'confirmed' | 'expired';
credentials?: {
bot_token: string;
ilink_bot_id: string;
ilink_user_id: string;
};
baseurl?: string;
}
export function getWeChatQRCode() {
return post<{ data: WeChatQRCodeResult }>('/api/v1/wechat/qrcode');
}
export function pollWeChatQRCodeStatus(qrcode: string) {
return post<{ data: WeChatQRCodeStatus }>('/api/v1/wechat/qrcode/status', { qrcode });
}
+263 -10
View File
@@ -89,13 +89,14 @@
<!-- Platform -->
<div class="form-item">
<label class="form-label">{{ $t('agentEditor.im.platform') }}</label>
<t-radio-group v-model="formData.platform" :disabled="!!editingChannel">
<t-radio-group v-model="formData.platform" :disabled="!!editingChannel" @change="onPlatformChange">
<t-radio-button value="wecom">{{ $t('agentEditor.im.wecom') }}</t-radio-button>
<t-radio-button value="feishu">{{ $t('agentEditor.im.feishu') }}</t-radio-button>
<t-radio-button value="slack">{{ $t('agentEditor.im.slack') }}</t-radio-button>
<t-radio-button value="telegram">{{ $t('agentEditor.im.telegram') }}</t-radio-button>
<t-radio-button value="dingtalk">{{ $t('agentEditor.im.dingtalk') }}</t-radio-button>
<t-radio-button value="mattermost">{{ $t('agentEditor.im.mattermost') }}</t-radio-button>
<t-radio-button value="wechat">{{ $t('agentEditor.im.wechat') }}</t-radio-button>
</t-radio-group>
</div>
@@ -105,8 +106,8 @@
<t-input v-model="formData.name" :placeholder="$t('agentEditor.im.channelNamePlaceholder')" />
</div>
<!-- Mode -->
<div class="form-item">
<!-- Mode (hidden for WeChat) -->
<div v-if="formData.platform !== 'wechat'" class="form-item">
<label class="form-label">{{ $t('agentEditor.im.mode') }}</label>
<t-radio-group v-model="formData.mode">
<t-radio-button value="websocket" :disabled="formData.platform === 'mattermost'">WebSocket</t-radio-button>
@@ -116,8 +117,8 @@
<p v-else class="form-hint">{{ $t('agentEditor.im.modeHint') }}</p>
</div>
<!-- Output mode -->
<div class="form-item">
<!-- Output mode (hidden for WeChat) -->
<div v-if="formData.platform !== 'wechat'" class="form-item">
<label class="form-label">{{ $t('agentEditor.im.outputMode') }}</label>
<t-radio-group v-model="formData.output_mode">
<t-radio-button value="stream">{{ $t('agentEditor.im.outputStream') }}</t-radio-button>
@@ -339,16 +340,62 @@
<p class="form-hint">{{ $t('agentEditor.im.mattermostPostToMainHint') }}</p>
</div>
</template>
<!-- WeChat credentials (QR code binding) -->
<template v-if="formData.platform === 'wechat'">
<p class="form-hint">{{ $t('agentEditor.im.wechatHint') }}</p>
<!-- Already bound state -->
<div v-if="wechatBound" class="wechat-bound-status">
<t-icon name="check-circle-filled" class="bound-icon" />
<span>{{ $t('agentEditor.im.wechatBindSuccess') }}</span>
<t-button size="small" variant="outline" theme="default" @click="startWeChatBinding">
{{ $t('agentEditor.im.wechatRebind') }}
</t-button>
</div>
<!-- QR code binding flow -->
<div v-else class="wechat-qr-section">
<!-- Initial state: show bind button -->
<div v-if="!wechatQRImgUrl" class="wechat-bind-action">
<t-button theme="default" variant="outline" :loading="wechatLoading" @click="startWeChatBinding">
<template #icon><t-icon name="scan" /></template>
{{ $t('agentEditor.im.wechatScanBind') }}
</t-button>
</div>
<!-- QR code displayed -->
<div v-else class="wechat-qr-display">
<div class="qr-container">
<img :src="wechatQRImgUrl" alt="WeChat QR Code" class="qr-image" />
<div v-if="wechatQRStatus === 'expired'" class="qr-expired-overlay" @click="startWeChatBinding">
<t-icon name="refresh" class="refresh-icon" />
<span>{{ $t('agentEditor.im.wechatQRExpired') }}</span>
</div>
</div>
<p class="qr-hint">
<template v-if="wechatQRStatus === 'scaned'">
{{ $t('agentEditor.im.wechatBinding') }}
</template>
<template v-else>
{{ $t('agentEditor.im.wechatScanning') }}
</template>
</p>
</div>
</div>
</template>
</div>
</t-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { ref, onMounted, watch, onUnmounted, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { MessagePlugin } from 'tdesign-vue-next';
import { listIMChannels, createIMChannel, updateIMChannel, deleteIMChannel, toggleIMChannel } from '@/api/agent';
import {
listIMChannels, createIMChannel, updateIMChannel, deleteIMChannel, toggleIMChannel,
getWeChatQRCode, pollWeChatQRCodeStatus,
} from '@/api/agent';
import { listKnowledgeBases } from '@/api/knowledge-base';
import type { IMChannel } from '@/api/agent';
@@ -366,12 +413,21 @@ const editingChannel = ref<IMChannel | null>(null);
// Knowledge base options for file-to-KB feature
const knowledgeBases = ref<{ id: string; name: string }[]>([]);
// WeChat QR code binding state
const wechatQRContent = ref(''); // raw text to encode as QR code
const wechatQRImgUrl = ref(''); // generated QR image URL
const wechatQRCode = ref(''); // opaque token for polling status
const wechatQRStatus = ref<string>('');
const wechatLoading = ref(false);
let wechatPollActive = false;
let wechatPollTimer: ReturnType<typeof setTimeout> | null = null;
const defaultCredentials = (): Record<string, any> => ({});
const formData = ref({
platform: 'wecom' as 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost',
platform: 'wecom' as 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost' | 'wechat',
name: '',
mode: 'websocket' as 'webhook' | 'websocket',
mode: 'websocket' as 'webhook' | 'websocket' | 'longpoll',
output_mode: 'stream' as 'stream' | 'full',
session_mode: 'user' as 'user' | 'thread',
knowledge_base_id: '',
@@ -401,6 +457,101 @@ watch(
}
},
);
// Whether WeChat credentials are already bound
const wechatBound = computed(() => {
return formData.value.platform === 'wechat' &&
formData.value.credentials.bot_token &&
formData.value.credentials.ilink_bot_id;
});
function onPlatformChange(val: string | number | boolean) {
formData.value.credentials = defaultCredentials();
stopWeChatPolling();
wechatQRContent.value = '';
wechatQRImgUrl.value = '';
wechatQRCode.value = '';
wechatQRStatus.value = '';
// WeChat uses fixed mode/output
if (val === 'wechat') {
formData.value.mode = 'longpoll';
formData.value.output_mode = 'full';
} else {
formData.value.mode = 'websocket';
formData.value.output_mode = 'stream';
}
}
async function startWeChatBinding() {
stopWeChatPolling();
wechatLoading.value = true;
wechatQRContent.value = '';
wechatQRImgUrl.value = '';
wechatQRStatus.value = '';
try {
const res = await getWeChatQRCode();
// qrcode_url is the text content to encode as QR code (e.g. a weixin:// URL)
wechatQRContent.value = res.data.qrcode_url;
wechatQRCode.value = res.data.qrcode;
wechatQRStatus.value = 'wait';
// Generate QR code image via public API (no extra npm dependency needed)
wechatQRImgUrl.value = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(res.data.qrcode_url)}`;
// Start long-polling for scan status
startStatusPolling();
} catch (e: any) {
MessagePlugin.error(e?.message || 'Failed to generate QR code');
} finally {
wechatLoading.value = false;
}
}
function startStatusPolling() {
wechatPollActive = true;
pollOnce();
}
async function pollOnce() {
if (!wechatPollActive) return;
try {
const statusRes = await pollWeChatQRCodeStatus(wechatQRCode.value);
if (!wechatPollActive) return;
wechatQRStatus.value = statusRes.data.status;
if (statusRes.data.status === 'confirmed' && statusRes.data.credentials) {
formData.value.credentials = {
bot_token: statusRes.data.credentials.bot_token,
ilink_bot_id: statusRes.data.credentials.ilink_bot_id,
ilink_user_id: statusRes.data.credentials.ilink_user_id,
};
stopWeChatPolling();
wechatQRContent.value = '';
wechatQRImgUrl.value = '';
MessagePlugin.success(t('agentEditor.im.wechatBindSuccess'));
return;
}
if (statusRes.data.status === 'expired') {
stopWeChatPolling();
return;
}
} catch {
// transient error
}
// Schedule next poll with a short delay (the backend already long-polled ~35s)
if (wechatPollActive) {
wechatPollTimer = setTimeout(pollOnce, 500);
}
}
function stopWeChatPolling() {
wechatPollActive = false;
if (wechatPollTimer) {
clearTimeout(wechatPollTimer);
wechatPollTimer = null;
}
}
async function loadChannels() {
loading.value = true;
@@ -461,6 +612,11 @@ function editChannel(channel: IMChannel) {
function resetForm() {
editingChannel.value = null;
stopWeChatPolling();
wechatQRContent.value = '';
wechatQRImgUrl.value = '';
wechatQRCode.value = '';
wechatQRStatus.value = '';
formData.value = {
platform: 'wecom',
name: '',
@@ -474,6 +630,12 @@ function resetForm() {
async function handleSave() {
try {
// For WeChat, validate that credentials are bound
if (formData.value.platform === 'wechat' && !formData.value.credentials.bot_token) {
MessagePlugin.warning(t('agentEditor.im.wechatScanBind'));
return;
}
if (editingChannel.value) {
await updateIMChannel(editingChannel.value.id, {
name: formData.value.name,
@@ -527,6 +689,10 @@ async function handleDelete(id: string) {
onMounted(() => {
loadChannels();
});
onUnmounted(() => {
stopWeChatPolling();
});
</script>
<style scoped lang="less">
@@ -670,6 +836,11 @@ onMounted(() => {
background: rgba(25, 42, 77, 0.08);
color: #192a4d;
}
&.wechat {
background: rgba(7, 193, 96, 0.08);
color: #07c160;
}
}
.channel-name {
@@ -815,4 +986,86 @@ onMounted(() => {
color: var(--td-text-color-placeholder);
}
}
</style>
// --- WeChat QR code binding ---
.wechat-bound-status {
display: flex;
align-items: center;
gap: 8px;
padding: 12px 16px;
background: rgba(7, 193, 96, 0.06);
border: 1px solid rgba(7, 193, 96, 0.2);
border-radius: 8px;
font-size: 14px;
color: var(--td-text-color-primary);
.bound-icon {
font-size: 18px;
color: #07c160;
}
}
.wechat-qr-section {
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
}
.wechat-bind-action {
padding: 24px 0;
}
.wechat-qr-display {
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
padding: 16px 0;
}
.qr-container {
position: relative;
width: 200px;
height: 200px;
border: 1px solid var(--td-component-stroke);
border-radius: 8px;
overflow: hidden;
// QR code images are always black-on-white; force white background
// so the code remains scannable in dark mode.
background: #fff;
.qr-image {
width: 100%;
height: 100%;
object-fit: contain;
}
}
.qr-expired-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
background: rgba(0, 0, 0, 0.6);
color: #fff;
cursor: pointer;
font-size: 12px;
.refresh-icon {
font-size: 24px;
}
}
.qr-hint {
font-size: 13px;
color: var(--td-text-color-secondary);
text-align: center;
}
</style>
+10 -1
View File
@@ -3314,12 +3314,14 @@ export default {
},
im: {
title: 'IM Integration',
description: 'Connect agent to instant messaging platforms like WeCom, Feishu, Slack, Telegram, DingTalk, and Mattermost',
description: 'Connect agent to instant messaging platforms like WeCom, Feishu, Slack, Telegram, DingTalk, Mattermost and WeChat',
feishu: 'Feishu',
slack: 'Slack',
telegram: 'Telegram',
dingtalk: 'DingTalk',
mattermost: 'Mattermost',
wecom: 'WeCom',
wechat: 'WeChat',
addChannel: 'Add Channel',
editChannel: 'Edit Channel',
deleteConfirm: 'Are you sure you want to delete this channel? This action cannot be undone.',
@@ -3354,6 +3356,13 @@ export default {
sessionModeUser: 'Per User (default)',
sessionModeThread: 'Per Thread',
sessionModeHint: 'User mode: each person has their own conversation. Use /clear to start fresh. Thread mode: each message thread is a separate conversation. Multiple people can collaborate in the same thread.',
wechatScanBind: 'Scan to bind WeChat',
wechatScanning: 'Scan the QR code with WeChat',
wechatBindSuccess: 'WeChat bound successfully',
wechatRebind: 'Rebind',
wechatHint: 'Requires iOS WeChat 8.0.70+, direct messages only',
wechatQRExpired: 'QR code expired, please try again',
wechatBinding: 'Binding...',
},
mcp: {
label: 'MCP Services',
+9 -1
View File
@@ -3289,13 +3289,14 @@ export default {
},
im: {
title: "IM 集成",
description: "将智能体接入即时通讯平台,支持企业微信、飞书、Slack、Telegram、钉钉和 Mattermost",
description: "将智能体接入即时通讯平台,支持企业微信、飞书、Slack、Telegram、钉钉、Mattermost和微信",
wecom: "企业微信",
feishu: "飞书",
slack: "Slack",
telegram: "Telegram",
dingtalk: "钉钉",
mattermost: "Mattermost",
wechat: "微信",
addChannel: "添加渠道",
editChannel: "编辑渠道",
deleteConfirm: "确定删除该渠道?删除后无法恢复。",
@@ -3330,6 +3331,13 @@ export default {
sessionModeUser: "按用户(默认)",
sessionModeThread: "按话题",
sessionModeHint: "用户模式:每个用户独立对话,使用 /clear 开始新对话。话题模式:每个消息话题独立对话,同一话题中多人可协作。",
wechatScanBind: "扫码绑定微信",
wechatScanning: "请使用微信扫描二维码",
wechatBindSuccess: "微信绑定成功",
wechatRebind: "重新绑定",
wechatHint: "需要 iOS 微信 8.0.70+ 版本,仅支持单聊消息",
wechatQRExpired: "二维码已过期,请重新获取",
wechatBinding: "绑定中...",
},
tools: {
thinking: "思考",