Browse Source

fix:edit whatsapp APIs

jimmyyem 1 month ago
parent
commit
2d0b7091a5

+ 36 - 6
desc/wechat/whatsapp.api

@@ -96,12 +96,11 @@ type (
         Data WhatsappInfo `json:"data"`
     }
 
-	ConversationalAutomationReq {
+	AutomationReq {
 		WaId  *string `json:"waId,optional"`
-		Cc *string `json:"cc,optional"`
 		Phone  *string `json:"phone,optional"`
 	}
-	ConversationalAutomationResp {
+	AutomationResp {
 		BaseDataInfo
 
 		// Whatsapp list data | Whatsapp列表数据
@@ -117,6 +116,25 @@ type (
 		CommandDescription string `json:"commandDescription,optional"`
 		CommandName string `json:"commandName,optional"`
 	}
+	SetAutomationReq {
+		EnableWelcomeMessage bool `json:"enableWelcomeMessage,optional"`
+		PhoneNumber string `json:"phoneNumber,optional"`
+		Commands []Command `json:"commands,optional"`
+		Prompts []string `json:"prompts,optional"`
+		WaId string `json:"waId,optional"`
+	}
+
+	sendCodeReq {
+		Phone  *string `json:"phone"`
+		WaId  *string `json:"waId"`
+		Method *string `json:"method"`
+		Locale *string `json:"locale"`
+	}
+	submitCodeReq {
+		Phone  *string `json:"phone"`
+		WaId  *string `json:"waId"`
+		Code *string `json:"code"`
+	}
 )
 
 @server(
@@ -146,7 +164,19 @@ service Wechat {
     @handler getWhatsappById
     post /whatsapp (IDReq) returns (WhatsappInfoResp)
 
-	// Get whatsapp by ID | 通过ID获取Whatsapp
-	@handler GetConversationalAutomation
-	post /whatsapp/GetConversationalAutomation (ConversationalAutomationReq) returns (ConversationalAutomationResp)
+	// 发送验证码
+	@handler sendWhatsappCode
+	post /whatsapp/sendCode (sendCodeReq) returns (BaseMsgResp)
+
+	// 提交验证信息
+	@handler submitWhatsappCode
+	post /whatsapp/submitCode (submitCodeReq) returns (BaseMsgResp)
+
+	// 获取开场白
+	@handler getAutomation
+	post /whatsapp/getAutomation (AutomationReq) returns (AutomationResp)
+
+	// 编辑开场白
+	@handler setAutomation
+	post /whatsapp/setAutomation (SetAutomationReq) returns (BaseMsgResp)
 }

+ 20 - 12
etc/wechat.yaml

@@ -6,8 +6,8 @@ Timeout: 30000
 Mode: "dev"
 
 Auth:
-  AccessSecret: LnQD46hBde0AgFXBer8ZZZe3FgC
-  AccessExpire: 259200
+  AccessSecret: jS6VKDtsJf3z1n2VKDtsJf3z1n2
+  AccessExpire: 2592000
 
 CROSConf:
   Address: '*'
@@ -17,7 +17,7 @@ Log:
   Mode: console
   Encoding: plain
   Stat: false
-  Path: /Users/songbowen/Downloads
+  Path: /tmp
   Level: info
   Compress: false
   KeepDays: 7
@@ -25,8 +25,8 @@ Log:
 
 DatabaseConf:
   Type: mysql
-  Host: localhost
-  Port: 3307
+  Host: mysql-server
+  Port: 3306
   DBName: wechat
   Username: root
   Password: simple-admin.
@@ -37,17 +37,17 @@ DatabaseConf:
 CoreRpc:
   # Target: k8s://default/core-rpc-svc:9101
   Endpoints:
-    - localhost:9101
+    - core-rpc:9101
   Enabled: true
 
 RedisConf:
-  Host: localhost:6380
+  Host: redis-server:6379
 
 CasbinDatabaseConf:
   Type: mysql
-  Host: localhost
-  Port: 3307
-  DBName: wechat-admin
+  Host: mysql-server
+  Port: 3306
+  DBName: wechat_admin
   Username: root
   Password: simple-admin.
   MaxOpenConn: 100
@@ -70,10 +70,18 @@ CasbinConf:
 Miniprogram:
   Appid: wx1452f34bba8fe718
   Secret: 171fdab212fdde0d51b59fa59c9ee070
-  redisaddr: redis-server:6379
+  Redisaddr: redis-server:6379
 
 Aliyun:
   ACCESS_KEY_ID: LTAI5tSJwCQyuaxXR3UxfnWw
   ACCESS_KEY_SECRET: 0pv4xhSPJv9IPSxrkB52FspJk27W7V
   OSS_ENDPOINT: sts.cn-beijing.aliyuncs.com
-  OSS_ROLEARN: acs:ram::1317798064750399:role/ramoss
+  OSS_ROLEARN: acs:ram::1317798064750399:role/ramoss
+
+Fastgpt:
+  BASE_URL: https://newapi.gkscrm.com/v1
+  API_KEY: sk-ZQRNypQOC8ID5WbpCdF263C58dF44271842e86D408Bb3848
+
+Xiaoice:
+  SubscriptionKey: ccaa5a6ff70f4393a934e69b9ace31bb
+  GptbotsAuthorization: app-hQL7oVq57McK5VBHlsMfhtUD

+ 70 - 2
hook/aliyun/whatsapp.go

@@ -52,8 +52,8 @@ func GetCamsAppId() (string, error) {
 	return *isvResponse.Body.AppId, nil
 }
 
-// GetConversationalAutomation 获取号码欢迎消息、开场白和命令
-func GetConversationalAutomation(custSpaceId, phone string) (*cams20200606.GetConversationalAutomationResponse, error) {
+// GetAutomation 获取号码欢迎消息、开场白和命令
+func GetAutomation(custSpaceId, phone string) (*cams20200606.GetConversationalAutomationResponse, error) {
 	client, _err := CreateCamsClient()
 	if _err != nil {
 		return nil, _err
@@ -72,6 +72,31 @@ func GetConversationalAutomation(custSpaceId, phone string) (*cams20200606.GetCo
 	return response, nil
 }
 
+// SetAutomation 设置号码欢迎消息、开场白和命令
+func SetAutomation(custSpaceId, phone string, prompts []string) (*cams20200606.UpdateConversationalAutomationResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	tmpPrompts := make([]*string, 0)
+	for _, v := range prompts {
+		tmpPrompts = append(tmpPrompts, &v)
+	}
+	request := &cams20200606.UpdateConversationalAutomationRequest{
+		CustSpaceId: tea.String(custSpaceId),
+		PhoneNumber: tea.String(phone),
+		Prompts:     tmpPrompts,
+	}
+
+	response, _err := client.UpdateConversationalAutomation(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}
+
 // AddCamsPhoneNumber 添加WhatsApp号码
 func AddCamsPhoneNumber(phone, cc, custSpaceId, verifiedName string) (*cams20200606.AddChatappPhoneNumberResponse, error) {
 	client, _err := CreateCamsClient()
@@ -93,3 +118,46 @@ func AddCamsPhoneNumber(phone, cc, custSpaceId, verifiedName string) (*cams20200
 
 	return response, nil
 }
+
+// SendCamsCode 发送验证码
+func SendCamsCode(phone, custSpaceId, method, locale string) (*cams20200606.GetChatappVerifyCodeResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.GetChatappVerifyCodeRequest{
+		PhoneNumber: tea.String(phone),
+		CustSpaceId: tea.String(custSpaceId),
+		Method:      tea.String(method),
+		Locale:      tea.String(locale),
+	}
+
+	response, _err := client.GetChatappVerifyCode(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}
+
+// SubmitCamsCode 提交验证信息
+func SubmitCamsCode(phone, code, custSpaceId string) (*cams20200606.ChatappVerifyAndRegisterResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.ChatappVerifyAndRegisterRequest{
+		PhoneNumber: tea.String(phone),
+		VerifyCode:  tea.String(code),
+		CustSpaceId: tea.String(custSpaceId),
+	}
+
+	response, _err := client.ChatappVerifyAndRegister(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}

+ 17 - 2
internal/handler/routes.go

@@ -1806,8 +1806,23 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
-					Path:    "/whatsapp/GetConversationalAutomation",
-					Handler: whatsapp.GetConversationalAutomationHandler(serverCtx),
+					Path:    "/whatsapp/sendCode",
+					Handler: whatsapp.SendWhatsappCodeHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/submitCode",
+					Handler: whatsapp.SubmitWhatsappCodeHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/getAutomation",
+					Handler: whatsapp.GetAutomationHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/setAutomation",
+					Handler: whatsapp.SetAutomationHandler(serverCtx),
 				},
 			}...,
 		),

+ 0 - 44
internal/handler/whatsapp/get_conversational_automation_handler.go

@@ -1,44 +0,0 @@
-package whatsapp
-
-import (
-	"net/http"
-
-	"github.com/zeromicro/go-zero/rest/httpx"
-
-	"wechat-api/internal/logic/whatsapp"
-	"wechat-api/internal/svc"
-	"wechat-api/internal/types"
-)
-
-// swagger:route post /whatsapp/GetConversationalAutomation whatsapp GetConversationalAutomation
-//
-// Get whatsapp by ID | 通过ID获取Whatsapp
-//
-// Get whatsapp by ID | 通过ID获取Whatsapp
-//
-// Parameters:
-//  + name: body
-//    require: true
-//    in: body
-//    type: ConversationalAutomationReq
-//
-// Responses:
-//  200: WhatsappInfoResp
-
-func GetConversationalAutomationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		var req types.ConversationalAutomationReq
-		if err := httpx.Parse(r, &req, true); err != nil {
-			httpx.ErrorCtx(r.Context(), w, err)
-			return
-		}
-
-		l := whatsapp.NewGetConversationalAutomationLogic(r.Context(), svcCtx)
-		resp, err := l.GetConversationalAutomation(&req)
-		if err != nil {
-			httpx.ErrorCtx(r.Context(), w, err)
-		} else {
-			httpx.OkJsonCtx(r.Context(), w, resp)
-		}
-	}
-}

+ 41 - 39
internal/logic/whatsapp/create_whatsapp_logic.go

@@ -2,11 +2,6 @@ package whatsapp
 
 import (
 	"context"
-	"wechat-api/ent"
-	"wechat-api/ent/whatsapp"
-	"wechat-api/internal/utils/dberrorhandler"
-
-	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 	"wechat-api/hook/aliyun"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
@@ -29,40 +24,47 @@ func NewCreateWhatsappLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cr
 }
 
 func (l *CreateWhatsappLogic) CreateWhatsapp(req *types.WhatsappInfo) (*types.BaseMsgResp, error) {
-	add, err := aliyun.AddCamsPhoneNumber(*req.PhoneName, *req.Cc, *req.WaId, *req.PhoneName)
-	//l.Logger.Infof("add=%v err=%v\n", add, err)
-	if err == nil && *add.Body.Code == "OK" {
-		var stringSlice []string
-
-		_, err := l.svcCtx.DB.Whatsapp.Query().
-			Where(whatsapp.WaID(*req.WaId)).
-			Where(whatsapp.Phone(*req.Phone)).
-			Where(whatsapp.Cc(*req.Cc)).
-			First(l.ctx)
-		if err != nil && ent.IsNotFound(err) {
-			_, err = l.svcCtx.DB.Whatsapp.Create().
-				SetNotNilWaID(req.WaId).
-				SetNotNilCallback(req.Callback).
-				SetNotNilAccount(req.Account).
-				SetNotNilPhone(req.Phone).
-				SetNotNilCc(req.Cc).
-				SetNotNilPhoneName(req.PhoneName).
-				SetNotNilPhoneStatus(req.PhoneStatus).
-				SetNotNilOrganizationID(req.OrganizationId).
-				SetNotNilAgentID(req.AgentId).
-				SetNotNilAPIBase(req.ApiBase).
-				SetNotNilAPIKey(req.ApiKey).
-				SetNotNilAllowList(stringSlice).
-				SetNotNilGroupAllowList(stringSlice).
-				SetNotNilBlockList(stringSlice).
-				SetNotNilGroupBlockList(stringSlice).
-				Save(l.ctx)
+	add, err := aliyun.AddCamsPhoneNumber(*req.Phone, *req.Cc, *req.WaId, *req.PhoneName)
+	l.Logger.Infof("add=%v err=%v\n", add, err)
+	//if err == nil && *add.Body.Code == "OK" {
+	//	var stringSlice []string
+	//
+	//	_, err := l.svcCtx.DB.Whatsapp.Query().
+	//		Where(whatsapp.WaID(*req.WaId)).
+	//		Where(whatsapp.Phone(*req.Phone)).
+	//		Where(whatsapp.Cc(*req.Cc)).
+	//		First(l.ctx)
+	//	if err != nil && ent.IsNotFound(err) {
+	//		_, err = l.svcCtx.DB.Whatsapp.Create().
+	//			SetNotNilWaID(req.WaId).
+	//			SetNotNilCallback(req.Callback).
+	//			SetNotNilAccount(req.Account).
+	//			SetNotNilPhone(req.Phone).
+	//			SetNotNilCc(req.Cc).
+	//			SetNotNilPhoneName(req.PhoneName).
+	//			SetNotNilPhoneStatus(req.PhoneStatus).
+	//			SetNotNilOrganizationID(req.OrganizationId).
+	//			SetNotNilAgentID(req.AgentId).
+	//			SetNotNilAPIBase(req.ApiBase).
+	//			SetNotNilAPIKey(req.ApiKey).
+	//			SetNotNilAllowList(stringSlice).
+	//			SetNotNilGroupAllowList(stringSlice).
+	//			SetNotNilBlockList(stringSlice).
+	//			SetNotNilGroupBlockList(stringSlice).
+	//			Save(l.ctx)
+	//
+	//		if err != nil {
+	//			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//		}
+	//	}
+	//}
 
-			if err != nil {
-				return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-			}
-		}
+	resp := types.BaseMsgResp{}
+	if err != nil {
+		resp.Msg = err.Error()
+		resp.Code = 1
+	} else {
+		resp.Msg = "添加成功"
 	}
-
-	return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
+	return &resp, nil
 }

+ 0 - 37
internal/logic/whatsapp/get_conversational_automation_logic.go

@@ -1,37 +0,0 @@
-package whatsapp
-
-import (
-	"context"
-	"wechat-api/hook/aliyun"
-
-	"wechat-api/internal/svc"
-	"wechat-api/internal/types"
-
-	"github.com/zeromicro/go-zero/core/logx"
-)
-
-type GetConversationalAutomationLogic struct {
-	logx.Logger
-	ctx    context.Context
-	svcCtx *svc.ServiceContext
-}
-
-func NewGetConversationalAutomationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetConversationalAutomationLogic {
-	return &GetConversationalAutomationLogic{
-		Logger: logx.WithContext(ctx),
-		ctx:    ctx,
-		svcCtx: svcCtx}
-}
-
-func (l *GetConversationalAutomationLogic) GetConversationalAutomation(req *types.ConversationalAutomationReq) (*types.ConversationalAutomationResp, error) {
-	phone := *req.Cc + *req.Phone
-	response, err := aliyun.GetConversationalAutomation(*req.WaId, phone)
-
-	resp := types.ConversationalAutomationResp{}
-	if err == nil && *response.Body.Code == "OK" {
-		//resp.Data.Commands = response.Body.Data.Commands
-
-	}
-
-	return &resp, nil
-}

+ 28 - 5
internal/types/types.go

@@ -3485,15 +3485,14 @@ type WhatsappInfoResp struct {
 	Data WhatsappInfo `json:"data"`
 }
 
-// swagger:model ConversationalAutomationReq
-type ConversationalAutomationReq struct {
+// swagger:model AutomationReq
+type AutomationReq struct {
 	WaId  *string `json:"waId,optional"`
-	Cc    *string `json:"cc,optional"`
 	Phone *string `json:"phone,optional"`
 }
 
-// swagger:model ConversationalAutomationResp
-type ConversationalAutomationResp struct {
+// swagger:model AutomationResp
+type AutomationResp struct {
 	BaseDataInfo
 	// Whatsapp list data | Whatsapp列表数据
 	Data ConversationalAutomationInfo `json:"data"`
@@ -3512,6 +3511,30 @@ type Command struct {
 	CommandName        string `json:"commandName,optional"`
 }
 
+// swagger:model SetAutomationReq
+type SetAutomationReq struct {
+	EnableWelcomeMessage bool      `json:"enableWelcomeMessage,optional"`
+	PhoneNumber          string    `json:"phoneNumber,optional"`
+	Commands             []Command `json:"commands,optional"`
+	Prompts              []string  `json:"prompts,optional"`
+	WaId                 string    `json:"waId,optional"`
+}
+
+// swagger:model sendCodeReq
+type SendCodeReq struct {
+	Phone  *string `json:"phone"`
+	WaId   *string `json:"waId"`
+	Method *string `json:"method"`
+	Locale *string `json:"locale"`
+}
+
+// swagger:model submitCodeReq
+type SubmitCodeReq struct {
+	Phone *string `json:"phone"`
+	WaId  *string `json:"waId"`
+	Code  *string `json:"code"`
+}
+
 // The data of whatsapp channel information | WhatsappChannel信息
 // swagger:model WhatsappChannelInfo
 type WhatsappChannelInfo struct {