Home > WeChat Applet > WeChat Development > China Unicom WeChat and DingTalk services

China Unicom WeChat and DingTalk services

coldplay.xixi
Release: 2020-08-31 17:39:59
forward
3155 people have browsed it

China Unicom WeChat and DingTalk services

[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.

WeChat Open Platform & DingTalk Open Platform

WeChat Public Account

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

DingTalk Open Platform

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

Translation service development

The translation service is very simple:

  • Get the text sent by the WeChat public account user
  • Call DingTalk AI translation interface , converted to the target language
  • Respond to the WeChat official account and send the translation results to the user

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)}
Copy after login

Running effect

##Conclusion

So far, between

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template