[Related learning recommendations: WeChat public account development tutorial】
WeChat and DingTalk belong to the two camps of Tencent and Alibaba. What kind of magical scene will it be when they can be interconnected?
Today, we will give it a try through the two open source Golang SDKs fastwego/offiaccount
and fastwego/dingding
:
Implement a WeChat public The service of Haomiaobian Translator: translate the Chinese text sent by the user into French.
Enable development mode and take over messages sent by users :
#At the same time, the appid/secret/token configuration information of the WeChat official account will also be obtained
Open the enterprise's internal H5 micro-application and complete the AI interface authorization:
At the same time, the configuration information of DingTalk AppKey/AppSecret will also be obtained
The translation service is very simple:
Main code:
// 微信公众账号var OffiAccount *offiaccount.OffiAccount// 钉钉 App 实例var DingApp *dingding.Appfunc init() { // 加载配置文件 viper.SetConfigFile(".env") _ = viper.ReadInConfig() // 创建公众号实例 OffiAccount = offiaccount.New(offiaccount.Config{ Appid: viper.GetString("APPID"), Secret: viper.GetString("SECRET"), }) // 创建钉钉应用实例 DingApp = dingding.NewApp(dingding.AppConfig{ AppKey: viper.GetString("AppKey"), AppSecret: viper.GetString("AppSecret"), })}func HandleMessage(c *gin.Context) { // 获取公众号消息 body, _ := ioutil.ReadAll(c.Request.Body) log.Println(string(body)) message, err := OffiAccount.Server.ParseXML(body) if err != nil { log.Println(err) } var output interface{} switch message.(type) { case type_message.MessageText: // 文本 消息 msg := message.(type_message.MessageText) // 调用 钉钉 翻译服务 params := struct { Query string `json:"query"` TargetLanguage string `json:"target_language"` SourceLanguage string `json:"source_language"` }{} params.Query = msg.Content params.SourceLanguage = "zh" params.TargetLanguage = "fr" data, err := json.Marshal(params) if err != nil { fmt.Println(string(data), err) return } // 翻译接口 resp, err := ai.Translate(DingApp, data) fmt.Println(string(resp), err) if err != nil { return } // 翻译结果 result := struct { Errcode int64 `json:"errcode"` Errmsg string `json:"errmsg"` Result string `json:"result"` }{} err = json.Unmarshal(resp, &result) fmt.Println(result, err) if err != nil { return } // 回复公众号 翻译结果文本消息 output = type_message.ReplyMessageText{ ReplyMessage: type_message.ReplyMessage{ ToUserName: type_message.CDATA(msg.FromUserName), FromUserName: type_message.CDATA(msg.ToUserName), CreateTime: strconv.FormatInt(time.Now().Unix(), 10), MsgType: type_message.ReplyMsgTypeText, }, Content: type_message.CDATA(result.Result), } } OffiAccount.Server.Response(c.Writer, c.Request, output)}
fastwego/offiaccount and
fastwego/dingding With the help of two open source Golang SDKs, we quickly realized the effect of connecting WeChat and DingTalk services.
The above is the detailed content of China Unicom WeChat and DingTalk services. For more information, please follow other related articles on the PHP Chinese website!