Wechat development series 4 – Send C4C Data change notification to Wechat app
- Wechat development series 1 – setup your development environment
- Wechat development series 2 – development Q&A service using nodejs
- Wechat development series 3 – Trigger C4C Account creation in Wechat app
- Wechat development series 4 – Send C4C Data change notification to Wechat app
- Wechat development series 5 – embedded your UI5 application to Wechat app
- Wechat development series 6 – Retrieve Wechat User info via oAuth2 and display it in UI5 application
- Wechat development series 7 – use Redis to store Wechat conversation history
Implementation detail
import AP.Common.GDT; import AP.FO.BusinessPartner.Global; [Extension] businessobject AP.FO.BusinessPartner.Global:Customer { // root node of Customer is not extendable node Common { [Label("Wechat ID")] element WechatID:LANGUAGEINDEPENDENT_EXTENDED_Text; } }
import ABSL; var common = this.Common.GetFirst(); if( common.WechatID.IsInitial()) { common.WechatID = common.Person.Name.FamilyName; }
3. Once the account is changed in C4C, a notification should be sent from C4C to our nodejs server. This automatic notification mechanism could be achieved via C4C OData event notification. See another of my blog Leverage C4C Odata notification to monitor C4C Opportunity change in CRM system for detail.
- field name: indivudualCustomerNewurl
- field value: https://
/sap/c4c/odata/cust/v1/zindividualcustomer/CustomerCommonCollection?$filter=
function notifyWechatUser(uuid,res){ console.log("begin to read uuid: " + uuid); _getAccount(uuid).then(function(wechatID){ res.status(200).end(); sendMessage(wechatID, "Dear user, A kind reminder: your C4C account is changed in the system."); }); }
Implementation of _getAccount
function _getAccount(uuid) { var AccountBOguid = uuid; var detailODataUrl = config.indivudualCustomerNewurl; var parentID = 'ParentObjectID eq '' + AccountBOguid + '''; detailODataUrl = detailODataUrl + encodeURI(parentID); var getOptions = { url: detailODataUrl, method: "GET", json:true, headers: { "content-type": "application/json", 'Authorization': 'Basic ' + new Buffer(config.credential).toString('base64') } }; return new Promise(function(resolve,reject){ var requestC = request.defaults({jar: true}); console.log("request with url: " + detailODataUrl); requestC(getOptions,function(error,response,body){ var wechatID = body.d.results[0].WechatID; console.log("wechat id: " + wechatID); resolve(wechatID); }); // end of requestC }); }
Implementation of sendMessage
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("error? " + error); console.log("response: " + response); console.log("data: " + data); console.log("response..............................................."); //printObject(response); console.log("data....................................................."); console.log("Status message: " + response.statusMessage); console.log("Data: " + data.errmsg); }); } module.exports = sendWCMeaasge;