Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript to send messages to Wechat users

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use JavaScript to send messages to Wechat users. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Concrete realization

After we log in to the console of Wechat official account, click Development-> basic configuration:

You can see the address of the Wechat message server we configured. In my first tutorial, we said that we develop a Web server locally with nodejs, then deploy it to your favorite cloud platform, such as Tencent Cloud, Aliyun, Baidu Cloud, etc. (I chose the cloud platform Heroku), and then maintain the url of the deployed application to the server configuration of the Wechat official account console, as shown in the following figure. After maintenance, Wechat users follow or send messages to the official account, which will be delivered to your nodejs application through Wechat platform, in which we can program to achieve some requirements. Hereafter, the nodejs application is called "message server".

We first use nodejs's express module to get an app object:

Var express = require ('express'); var app = express ()

When Wechat users follow your official account, Wechat platform will send a HTTP post request to your message server. You need to programmatically respond to this post request.

App.route ('/') .post (function (req,res) {var content; function / store the contents of HTTP post sent by Wechat platform in the variable content ("data", function (data) {content = data.toString ("utf-8");}); req.on ("end", function () {console.log ("new http post:" + content)) / / print HTTP post request for debugging

/ / parse the event object from the HTTP request sent by Wechat platform. If it is a fan spot, the event type is subscribe.

Var msgType = formattedValue (getXMLNodeValue ('MsgType', content)); / / fans clicked the follow button and if (event = = "subscribe") {/ / replied to a welcome message to fans var replyxml = replyMessage (content, "Welcome, finally here you are!") ; res.send (replyxml);}}

The logic of the above code is so clear that it is easy to understand the comments. The key is how to reply the welcome message to the fans who clicked the follow button.

The core logic is in the replyMessage function, which parses the fans' openID from the HTTP post content sent to the message server by the Wechat platform. The code is as follows:

Input parameter 1: all the contents of the HTTP post sent to the message server by Wechat platform

Input parameter 2: welcome message ready to be pushed to fans

Output parameters: the Wechat message of the welcome message to be returned to fans through HTTP should conform to the message specification defined by Wechat, as shown in the following code.

Module.exports = function (originalBody, contentToReply) {/ / the receiver of the message extracted from the original message var ToUserName = getXMLNodeValue ('ToUserName', originalBody); / / the sender of the message extracted from the original message var FromUserName = getXMLNodeValue (' FromUserName',originalBody); var CreateTime = getXMLNodeValue ('CreateTime',originalBody); / / tells the Wechat platform that the message type is a text message var MsgType = "" / / prepare to add the text content of the welcome message to the message message var Content = contentToReply; / / start assembling the message message to be sent to Wechat fans var xml ='+ FromUserName+''+ToUserName+''+CreateTime+'' + MsgType +''+ Content+''; console.log ("xml to be sent:" + xml); / / print the message message return xml; / / return message message} Thank you for reading! This is the end of the article on "how to use JavaScript to send messages to Wechat users". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report