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
+23 -16
View File
@@ -14,6 +14,7 @@ import (
// validIMPlatforms is the set of supported IM platforms.
var validIMPlatforms = map[string]bool{
"wecom": true, "feishu": true, "slack": true, "telegram": true, "dingtalk": true, "mattermost": true,
"wechat": true,
}
// IMHandler handles IM platform callback requests and channel CRUD.
@@ -45,13 +46,13 @@ func (h *IMHandler) CreateIMChannel(c *gin.Context) {
}
var req struct {
Platform string `json:"platform" binding:"required"`
Name string `json:"name"`
Mode string `json:"mode"`
OutputMode string `json:"output_mode"`
KnowledgeBaseID string `json:"knowledge_base_id"`
Credentials types.JSON `json:"credentials"`
Enabled *bool `json:"enabled"`
Platform string `json:"platform" binding:"required"`
Name string `json:"name"`
Mode string `json:"mode"`
OutputMode string `json:"output_mode"`
KnowledgeBaseID string `json:"knowledge_base_id"`
Credentials types.JSON `json:"credentials"`
Enabled *bool `json:"enabled"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@@ -59,7 +60,7 @@ func (h *IMHandler) CreateIMChannel(c *gin.Context) {
}
if !validIMPlatforms[req.Platform] {
c.JSON(http.StatusBadRequest, gin.H{"error": "platform must be 'wecom', 'feishu', 'slack', 'telegram', 'dingtalk' or 'mattermost'"})
c.JSON(http.StatusBadRequest, gin.H{"error": "platform must be 'wecom', 'feishu', 'slack', 'telegram', 'dingtalk', 'mattermost' or 'wechat'"})
return
}
@@ -77,15 +78,21 @@ func (h *IMHandler) CreateIMChannel(c *gin.Context) {
if req.Enabled != nil {
channel.Enabled = *req.Enabled
}
if channel.Mode == "" {
if channel.Platform == "mattermost" {
channel.Mode = "webhook"
} else {
channel.Mode = "websocket"
// WeChat uses long-polling mode and full output only
if req.Platform == "wechat" {
channel.Mode = "longpoll"
channel.OutputMode = "full"
} else {
if channel.Mode == "" {
if channel.Platform == "mattermost" {
channel.Mode = "webhook"
} else {
channel.Mode = "websocket"
}
}
if channel.OutputMode == "" {
channel.OutputMode = "stream"
}
}
if channel.OutputMode == "" {
channel.OutputMode = "stream"
}
if channel.Credentials == nil {
channel.Credentials = types.JSON("{}")
+72
View File
@@ -0,0 +1,72 @@
package handler
import (
"net/http"
"github.com/Tencent/WeKnora/internal/im/wechat"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/gin-gonic/gin"
)
// qrCodeService is a singleton for WeChat QR code operations.
var qrCodeService = wechat.NewQRCodeService()
// WeChatGetQRCode generates a QR code for WeChat login.
// POST /api/v1/wechat/qrcode
func (h *IMHandler) WeChatGetQRCode(c *gin.Context) {
ctx := c.Request.Context()
result, err := qrCodeService.GetLoginQRCode(ctx)
if err != nil {
logger.Errorf(ctx, "[WeChat] Failed to generate QR code: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate QR code: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": gin.H{
"qrcode_url": result.QRCodeURL,
"qrcode": result.QRCode,
},
})
}
// WeChatPollQRCodeStatus checks the scan status of a WeChat QR code.
// POST /api/v1/wechat/qrcode/status
func (h *IMHandler) WeChatPollQRCodeStatus(c *gin.Context) {
ctx := c.Request.Context()
var req struct {
QRCode string `json:"qrcode" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "qrcode is required"})
return
}
result, err := qrCodeService.PollQRCodeStatus(ctx, req.QRCode)
if err != nil {
logger.Errorf(ctx, "[WeChat] Failed to poll QR code status: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to check QR code status"})
return
}
resp := gin.H{
"status": result.Status,
}
// Only include credentials when login is confirmed
if result.Status == "confirmed" {
resp["credentials"] = gin.H{
"bot_token": result.BotToken,
"ilink_bot_id": result.ILinkBotID,
"ilink_user_id": result.ILinkUserID,
}
// Include baseurl if the server returned one (may override default)
if result.BaseURL != "" {
resp["baseurl"] = result.BaseURL
}
}
c.JSON(http.StatusOK, gin.H{"data": resp})
}