Google的RCS业务消息:Node.js客户端
RCS 业务消息 以品牌,丰富的媒体,互动,分析等手段升级短信. 有了RCS,企业可以将品牌,互动的移动体验带入本地的Android短信应用.
本文件载有: API 参考,样品,以及开发Node.js应用的其他资源. 需要额外帮助开发RCS BusinessMessaging应用程序,使用Node.js和其他语言,参见我们 RCS 商业消息快速启动 向导.
文档
RCS 业务通讯 API 的文档可以找到 这里。 。 。 .
快速启动
在你开始之前
- 与 RCS 业务通讯注册。 。 。 .
添加依赖关系
在您的依赖性部分添加以下内容: package.json 数字 :
"dependencies": {
...
"@google/rcsbusinessmessaging": "^1.0.10"
...
},
我们建议您使用最新版本 输入 npm 仓库。 。 。 .
样本使用情况
以下样本假设一个类似的库初始化,如 使用客户端库 节.
初始化
您通过提供您的服务账户密钥和代理代理ID来创建库 .
const SERVICE_ACCOUNT_PUBLIC_KEY = 'REPLACE_ME';
// Get the RCS Business Messaging client library helper
const rbmApiHelper = require('@google/rcsbusinessmessaging');
rbmApiHelper.initRbmApi(SERVICE_ACCOUNT_PUBLIC_KEY);
rbmApiHelper.setAgentId('myrbmagent'); // my agent id was [email protected]
自1.0.8发布以来,您可以选择将库禁用 特定区域终点。 这可以改善业绩,取决于 关于您代码的地理位置.
rbmApiHelper.initRbmApi(privateKey);
// The RBM API can now be initialised to use a specific regional
// entry point.
// rbmApiHelper.initRbmApi(privateKey, rbmApiHelper.REGION_APAC);
// rbmApiHelper.initRbmApi(privateKey, rbmApiHelper.REGION_EU);
// rbmApiHelper.initRbmApi(privateKey, rbmApiHelper.REGION_US);
发送附有建议的答复和行动的短信
// Create the payload for sending a message of "Hello, World!" along with
// a suggested reply and two suggested actions
rbmApiHelper.sendMessage({
messageText: 'Hello, World!',
msisdn: phoneNumber,
suggestions: [
{
reply: {
text: 'Sample Chip',
postbackData: 'sample_chip',
},
},
{
action: {
text: 'URL Action',
postbackData: 'url_action',
openUrlAction: {
url: 'https://www.google.com',
},
},
},
{
action: {
text: 'Dial Action',
postbackData: 'dial_action',
dialAction: {
phoneNumber: '+12223334444',
},
},
},
]
}
);
发一张富豪卡
// Create the payload for sending a rich card
rbmApiHelper.sendRichCard({
messageText: 'Hello, world!',
messageDescription: 'RBM is awesome!',
msisdn: phoneNumber,
suggestions: [
{
reply: {
'text': 'Suggestion #1',
'postbackData': 'suggestion_1',
},
},
{
reply: {
'text': 'Suggestion #2',
'postbackData': 'suggestion_2',
},
},
],
imageUrl: 'http://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif',
height: 'TALL',
});
送木马
// Create the payload for sending a carousel
rbmApiHelper.sendCarouselCard({
msisdn: phoneNumber,
cardContents: [
{
title: 'Card #1',
description: 'The description for card #1',
suggestions: [
{
reply: {
text: 'Card #1',
postbackData: 'card_1',
}
}
],
media: {
height: 'MEDIUM',
contentInfo: {
fileUrl: 'https://storage.googleapis.com/kitchen-sink-sample-images/cute-dog.jpg',
forceRefresh: false,
},
},
},
{
title: 'Card #2',
description: 'The description for card #2',
suggestions: [
{
reply: {
text: 'Card #2',
postbackData: 'card_2',
}
}
],
media: {
height: 'MEDIUM',
contentInfo: {
fileUrl: 'https://storage.googleapis.com/kitchen-sink-sample-images/elephant.jpg',
forceRefresh: false,
},
},
},
});
发送带有生命时间的信息
发送按成果管理信息,如果时间已到,将取消 在交付之前 已添加至第1.0.3版.
const params = {
messageText: 'This message will expire!',
msisdn: config.phoneNumber,
// allow 10 seconds for message delivery
timeToLive: '10s',
};
rbmApiHelper.sendMessage(params,
function(response, err) {
if (err !== undefined) {
console.log(
util.inspect(err, {showHidden: false, depth: null, colors: true}));
}
if (response !== undefined) {
console.log(
util.inspect(response, {showHidden: false, depth: null, colors: true}));
}
}
);
用过期时间发送信件
发送将取消的成果管理制消息,如果到期时间已到 在交付之前 已添加至第1.0.3版.
// Calculate UTC time in zulu format 20 seconds from now
const d = new Date();
d.setSeconds(d.getSeconds()+20);
const params = {
messageText: 'This message will expire!',
msisdn: config.phoneNumber,
expireTime: d.toISOString()
};
rbmApiHelper.sendMessage(params,
function(response, err) {
if (err !== undefined) {
console.log(
util.inspect(err, {showHidden: false, depth: null, colors: true}));
}
if (response !== undefined) {
console.log(
util.inspect(response, {showHidden: false, depth: null, colors: true}));
}
}
);
上传文件到 RMB 内容存储
支持在发布1.0.6.中添加文件上传.
const params = {
fileUrl: 'https://storage.googleapis.com/kitchen-sink-sample-images/cute-dog.jpg',
thumbnailUrl: 'https://storage.googleapis.com/kitchen-sink-sample-images/elephant.jpg',
// contentDescription: string
};
rbmApiHelper.uploadFile(params,
function(response, err) {
if (err !== undefined) {
console.log(
util.inspect(err, {showHidden: false, depth: null, colors: true}));
}
if (response !== undefined) {
console.log(
util.inspect(response, {showHidden: false, depth: null, colors: true}));
}
}
);
发送先前上传的文件
第1.0.6.版中增加了支持内容.
const params = {
// Uploaded file id. If this uplod also contains a thumbnail, it will be used
uploadedFileName: 'files/lpcg6f1tnYg1ryaQNho6hjka',
// Uploaded thumbnail id. If this upload contains a thumbnail then it will be
// used in preference to the one above.
// uploadedThumbnailName: "files/lpchblt9boYTjT8ck6UGQ8uK",
msisdn: config.phoneNumber,
};
rbmApiHelper.sendMessage(params,
function(response, err) {
if (err !== undefined) {
console.log(
util.inspect(err, {showHidden: false, depth: null, colors: true}));
}
if (response !== undefined) {
console.log(
util.inspect(response, {showHidden: false, depth: null, colors: true}));
}
}
);
版本
这个图书馆跟随 语义版本。 。 。 .
这个图书馆被认为是 一般可用性(GA)这表示 稳定; 代码表面不会改变向后兼容的方式 除非绝对必要(例如,由于关键的安全问题)或 大量贬值期。 问题和要求 大会 图书馆 以最高优先处理.
捐款
欢迎捐款! 见 贡献指南。 。 。 .
许可证
Apache 版本 2.0
见 自由主义
已知问题
在最近版本的节点.js上,您可能看到这个贬值警告:
(node:23043) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
这是因为在基本法律中存在依赖性 googleapis 图书馆。 这事会解决的
当负责这个图书馆的团队 运送更新版.