search
HomeWeChat AppletWeChat DevelopmentChina Unicom WeChat and DingTalk services

China Unicom WeChat and DingTalk services

Aug 31, 2020 pm 05:39 PM
WeChatDingTalk

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)}

Running effect

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!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool