首页
学习
活动
专区
圈层
工具
发布

Google App脚本中的Nexmo API调用

Google Apps Script 中调用 Nexmo API 的完整指南

基础概念

Nexmo API(现为Vonage API的一部分)是一组通信API,允许开发者通过短信、语音、验证等功能集成通信能力到应用中。Google Apps Script (GAS)是Google提供的基于JavaScript的云脚本语言,用于扩展Google Workspace应用功能。

相关优势

  1. 无需服务器:直接在Google云端运行脚本
  2. 快速集成:与Google Sheets、Docs等无缝集成
  3. 自动化工作流:可定时触发或基于事件触发
  4. 免费配额:Google Apps Script有免费执行配额

调用Nexmo API的类型

  1. 短信API
  2. 语音API
  3. 验证API
  4. 号码洞察API

应用场景

  • 通过Google表单提交自动发送短信确认
  • 从Google Sheets批量发送短信通知
  • 基于Google日历事件触发语音提醒
  • 两步验证集成到Google Workspace应用

实现代码示例

1. 发送短信示例

代码语言:txt
复制
function sendSMS() {
  var apiKey = 'YOUR_NEXMO_API_KEY';
  var apiSecret = 'YOUR_NEXMO_API_SECRET';
  var fromNumber = 'NEXMO_VIRTUAL_NUMBER';
  var toNumber = 'RECIPIENT_NUMBER';
  var messageText = 'Hello from Google Apps Script!';
  
  var url = 'https://rest.nexmo.com/sms/json?' +
    'api_key=' + encodeURIComponent(apiKey) +
    '&api_secret=' + encodeURIComponent(apiSecret) +
    '&from=' + encodeURIComponent(fromNumber) +
    '&to=' + encodeURIComponent(toNumber) +
    '&text=' + encodeURIComponent(messageText);
  
  var response = UrlFetchApp.fetch(url, {
    method: 'GET',
    muteHttpExceptions: true
  });
  
  var result = JSON.parse(response.getContentText());
  Logger.log(result);
  
  if (result.messages[0].status === '0') {
    Logger.log('Message sent successfully');
  } else {
    Logger.log('Message failed with error: ' + result.messages[0]['error-text']);
  }
}

2. 验证API示例

代码语言:txt
复制
function startVerification() {
  var apiKey = 'YOUR_NEXMO_API_KEY';
  var apiSecret = 'YOUR_NEXMO_API_SECRET';
  var phoneNumber = 'USER_PHONE_NUMBER';
  var brandName = 'YOUR_APP_NAME';
  
  var url = 'https://api.nexmo.com/verify/json?' +
    'api_key=' + encodeURIComponent(apiKey) +
    '&api_secret=' + encodeURIComponent(apiSecret) +
    '&number=' + encodeURIComponent(phoneNumber) +
    '&brand=' + encodeURIComponent(brandName);
  
  var response = UrlFetchApp.fetch(url, {
    method: 'GET',
    muteHttpExceptions: true
  });
  
  var result = JSON.parse(response.getContentText());
  
  if (result.status === '0') {
    Logger.log('Verification request sent, request_id: ' + result.request_id);
    return result.request_id;
  } else {
    Logger.log('Error: ' + result.error_text);
    return null;
  }
}

function checkVerification(requestId, code) {
  var apiKey = 'YOUR_NEXMO_API_KEY';
  var apiSecret = 'YOUR_NEXMO_API_SECRET';
  
  var url = 'https://api.nexmo.com/verify/check/json?' +
    'api_key=' + encodeURIComponent(apiKey) +
    '&api_secret=' + encodeURIComponent(apiSecret) +
    '&request_id=' + encodeURIComponent(requestId) +
    '&code=' + encodeURIComponent(code);
  
  var response = UrlFetchApp.fetch(url, {
    method: 'GET',
    muteHttpExceptions: true
  });
  
  var result = JSON.parse(response.getContentText());
  
  if (result.status === '0') {
    Logger.log('Verification successful');
    return true;
  } else {
    Logger.log('Verification failed: ' + result.error_text);
    return false;
  }
}

常见问题及解决方案

1. 请求失败

原因

  • API密钥或密钥不正确
  • 配额用尽
  • 电话号码格式错误

解决

  • 检查API凭证
  • 验证电话号码格式(包括国家代码)
  • 检查Nexmo账户余额或配额

2. 响应解析错误

原因

  • API返回非JSON格式
  • 网络问题导致响应不完整

解决

  • 添加错误处理
  • 检查muteHttpExceptions设置
  • 实现重试机制
代码语言:txt
复制
try {
  var response = UrlFetchApp.fetch(url, {
    method: 'GET',
    muteHttpExceptions: true
  });
  var result = JSON.parse(response.getContentText());
} catch (e) {
  Logger.log('Error parsing response: ' + e);
}

3. 执行时间过长

原因

  • 网络延迟
  • Google Apps Script执行时间限制(6分钟)

解决

  • 优化代码,减少不必要的操作
  • 考虑将长时间运行的任务分解为多个触发器

4. 权限问题

原因

  • 脚本没有访问外部URL的权限

解决

  • 确保脚本已授权访问外部服务
  • 在脚本编辑器中:资源 > 云平台项目 > 启用高级Google服务

最佳实践

  1. 安全存储凭证:使用Google Apps Script的PropertiesService存储敏感信息
  2. 安全存储凭证:使用Google Apps Script的PropertiesService存储敏感信息
  3. 错误处理:实现全面的错误处理和日志记录
  4. 速率限制:遵守Nexmo API的速率限制,必要时添加延迟
  5. 测试:在发送大量消息前先进行小规模测试
  6. 国际化:处理不同国家的电话号码格式和编码

通过以上方法和示例,您可以在Google Apps Script中有效地集成Nexmo API,实现各种通信功能。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券