微信开发系列之三 - 在微信公众号里发起SAP C4C Account的创建

简介: 微信开发系列之三 - 在微信公众号里发起SAP C4C Account的创建

Tencent’s WeChat, a social networking app with more than 760 million monthly active users, is becoming a dominant mobile channel connecting businesses and customers.

In previous blogs we have already setup the environment for Wechat development and build some toy services to get familar with overall process.


In this blog, we will implement some feature which interacts with C4C system.


The implementation contains purely nodejs development via Javascript and do not need any development in C4C side.


Implemented feature

Here below is my testing subscription account. When I scan it via my Wechat app,


I can click the Green button “关注” ( subscribe ) to finish subscription to this account.

image.pngimage.pngimage.pngimage.pngYou must maintain a valid user name and password which could have access to create new individual customer in your C4C system.

The access token will be used when you try to send a message to an user who has subscribed your Wechat account via Wechat Restful API. It will expire by default 2 hours after generation. The token could be refreshed based on appid and secret. For simplification purpose I just generate the token and store it in configuration file.


(2) Once a Wechat user presses “subscribe” button, an event with HTTP post will be sent to the Wechat server which is bound to your subscription account. As a result we have to react to this post request, parse the Wechat ID which has clicked the “subscribe” button, and create a new individual customer in C4C system based on this Wechat ID.


Here below is the source code how we react to the event with event key “subscribe”.

(1) the welcome message “Welcome to Jerry’s subscription account” is hard coded;


(2) The Wechat ID of user who has finished subscription is stored in variable fromUserName

image.png

var config = require("../../config.js");
var request = require('request');
var postWCMessage = require("./postMessageToUser.js");
var getTokenOptions = {
        url: config.individualCustomerurl,
        method: "GET",
        json:true,
        headers: {
            "content-type": "application/json",
            'Authorization': 'Basic ' + new Buffer(config.credential).toString('base64'),
            "x-csrf-token" :"fetch"
        }
};
function getToken() {
  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      requestC(getTokenOptions,function(error,response,body){
       var csrfToken = response.headers['x-csrf-token'];
       if(!csrfToken){
          reject({message:"token fetch error"});
          return;
       }
       resolve(csrfToken);
      }); // end of requestC
     });
}
function _createIndividualCustomer(token, fromUserName){
  return new Promise(function(resolve, reject){
    var oPostData = {
      "FirstName":"Wechat",
      "LastName":fromUserName,
    "RoleCode": "ZCRM01",
    "CountryCode": "US",
    "StatusCode": "2"
    };
    var requestC = request.defaults({jar: true});
        var createOptions = {
              url: config.individualCustomerurl,
              method: "POST",
              json:true,
              headers: {
                  "content-type": "application/json",
                  'x-csrf-token': token
              },
              body:oPostData
        };
        requestC(createOptions,function(error,response,data){
            if(error){
                reject(error.message);
            }else {
               resolve(data);
            }
        });// end of requestC
  });
}
module.exports = function createAccount(fromUserName){
  getToken().then(function(token) {
  console.log("token received: " + token);
  _createIndividualCustomer(token, fromUserName).then(function(data){
    var message = "account created: " + data.d.results.CustomerID;
    console.log(message);
    postWCMessage(fromUserName, message);
  });
});
};

image.png

var config = require("../../config.js");
var request = require("request");
function printObject(oData){
  for( var a in oData){
    console.log("key: " + a);
    console.log("value: " + oData[a]);
    if( typeof oData[a] === "object"){
      printObject(oData[a]);
    }
  }
}
function sendWCMeaasge(toUser,sMessage){
  console.log("begin to send message to user: " + toUser + " with message: " + sMessage);
    var options = {
            url:"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" +
            config.accessToken, 
            method: "POST",
            json:true,
            headers: {
              "content-type": "application/json"},
            body:{
              "touser":toUser,
              "msgtype":"text",
              "text":
              {
                   "content":sMessage
              }
                }
          };
      request(options,function(error,response,data){
        console.log("Status message: " + response.statusMessage);
        console.log("Data: " + data.errmsg);
      });
  }
module.exports = sendWCMeaasge;

It is very convenient to use this Restful API to send message to a given Wechat user who has subscribed the testing account. You could test it in postman:

image.pngimage.png

相关文章
|
5月前
|
小程序 PHP
微信公众号开发(一)打通服务器与微信之间的通信
说来惭愧PHP做了这么久,好像就没有从头开发过一个微信公众号,这次刚好有机会从头接入开发一个完整的公众号,也不能说完整,但是这些微信的接口我基本上都试一试~看看大概是什么情况。 首先:打通服务器与微信之间的通信。
62 0
|
5月前
|
前端开发 开发者
【微信公众号对接】有关签名一直报错,提示invalid signature问题(我的签名和使用微信开发者工具验证返回的签名的是一致的)但还是报错!!!
【微信公众号对接】有关签名一直报错,提示invalid signature问题(我的签名和使用微信开发者工具验证返回的签名的是一致的)但还是报错!!!
53 0
|
5月前
|
XML 移动开发 小程序
微信公众号开发(七)微信h5跳转小程序及小游戏示例
最近公司做活动,需要从h5页面跳转至微信小游戏。 当时接到这个需求的时候,就在想,这玩意能相互跳转么? 后来百度了一下,还真行。
121 1
|
5月前
|
小程序 PHP
微信公众号开发(六)微信支付(发红包、企业支付到零钱)需要证书请求示例
这里最主要的就是curlpost请求的时候需要带上证书。否则请求会失败。
72 0
|
5月前
|
JSON 小程序 数据库
微信公众号开发(二)微信公众号的access_token
微信对用户使用开放了很多的功能,如:自定义菜单接口、客服接口、获取用户信息接口、用户分组接口、群发接口,但是为了保证用户访问这些功能相对安全,每次访问都需要带上一个秘钥去验证身份。那么这个秘钥就是access_token。
74 0
|
7月前
|
XML JavaScript 测试技术
如何打通 SAP Cloud for Customer 系统和微信公众号的双向消息通信功能(二)
如何打通 SAP Cloud for Customer 系统和微信公众号的双向消息通信功能
64 0
|
7月前
|
JavaScript 前端开发 API
如何打通 SAP Cloud for Customer 系统和微信公众号的双向消息通信功能(一)
如何打通 SAP Cloud for Customer 系统和微信公众号的双向消息通信功能
44 0
|
8月前
|
SQL JavaScript 前端开发
SAP Field Service Management 和微信集成的案例分享和实现介绍
SAP Field Service Management 和微信集成的案例分享和实现介绍
63 0
微信公众号项目调起微信支付等
微信公众号项目调起微信支付等
|
10月前
|
小程序
微信:微信小程序、微信公众号--注册、关联(小建议)
微信:微信小程序、微信公众号--注册、关联(小建议)
89 0

热门文章

最新文章