用 Google App Script 实现发送认证码的 API

昨天用 Vite 快速打造了输入信箱获取认证码的页面,但必须搭配发送认证码的 API 才能继续完成这个 LIFF APP。因为会用到发信功能,所以还是选择 Google App Script 作为开发 API 的工具吧!

新建 GAS 专案 Send Mail

将 Read Mail 做为资料库载入

因为要用到 Read Mail 连接 google sheet 的功能,所以需要将 Read Mail 做为资料库载入。忘记怎麽操作的话可以参考 将先前的 GAS 专案部署为资料库

API 规划

method: POST
postdata: {
    name: string
    mail: string
    token: string(line user id)
}

文件

Gmail App - Send Mail

sendMail.gs

要完成的事情有三件:

  1. 接收 POST Request
  2. 找到未使用的认证码,押上 user id, 发送日期, 有效时戳
  3. 将步骤2的认证码寄送到指定的 email

doPost

因为这是要作为 API 使用,所以需要 doPost function 处理 post request

function doPost(e) {
  var message = "fail";
  var input = JSON.parse(e.postData.contents);
  // postdata这几项必须不为空
  // 也可以多用正则检验 mail 是否为有效信箱地址,这边先略过
  if (input.name && input.mail && input.token) {
    // 取得未使用的认证码
    var verificationCode = getVerificationCode(input.token, input.name, input.mail);
    if (verificationCode.length > 0) {
      // 组合信件内容文字
      var content = `${input.name} 您好,您的身份认证码为: ${verificationCode},时效为10分钟。`;
      // 信件标题采用此种格式,让使用者一眼就能从标题快速找到认证码
      sendMail(input.mail, `[${verificationCode}] 验证码小帮手的身份认证码`, content);
      // 回应讯息标为成功
      message = "success";
    }
  }
  // 回应给 client 端的讯息
  return ContentService.createTextOutput(JSON.stringify({message})).setMimeType(ContentService.MimeType.JSON);
}

修改 verification_code sheet

因为我们需要回写 user id, 发送日期, 有效时戳等资料,所以要修改 verification_code 这张 sheet 格式如下图:
modify_verification_code_sheet

getVerificationCode

接着实作 取得未使用的认证码 功能,这边会用到前面载入的 ReadMail 资料库去连接 Google Sheet,并且新载入 ReplyMessage 做为资料库使用 (如果忘记怎麽设定载入,请参考 部署 Google App Script 专案(1))

function getVerificationCode(userId, name, email) {
  var sheet = ReadMail.connectToSheet('verification_code');
  // 取出所有验证码的前两行
  var codes = sheet.getSheetValues(2, 1, (sheet.getLastRow()-1), 2);
  var verificationCode = "";
  codes.some((value, index) => {
    // 逐列搜寻,直到找到 user_id 为空,表示此未被使用过的认证码
    if(value[1].length === 0) {
      // 取得认证码的值
      verificationCode = value[0];
      const now = new Date();
      const expireAt = Math.floor(now.getTime() / 1000)+600;
      var rowRange = sheet.getRange((index+2), 2, 1, 4);
      // 在 verification_code sheet 中押上 user id, 发送日期, 有效时戳
      rowRange.setValues([[userId,, now, expireAt]]);
      // 在 users sheet 中押上 user id, name, email
      ReplyMessage.upsertUserInfo(userId, {name, email})
      return true;
    }
  });
  return verificationCode;
}

sendMail

最後就是要实作寄送认证码信件的功能,使用 GmailApp 即可轻松达成

function sendMail(to, title, content) {
  GmailApp.sendEmail(to, title, content, {name: '验证码小帮手'});
}

将 Send Mail 部署为 Web App

忘记操作方法的话可以参考 部署 Google App Script 专案(2) & Line Bot 简单回应讯息

部署好後取得网页应用程序的网址後,就可以用 PostMan 搭配以下范例 json 试打看看这个 API 是否能正常运作。

{
    "name": "user name",
    "mail": "user email",
    "token": "test token"
}

以上~今天完成了发送认证码的 API,明天就可以继续在 LIFF APP 完成串接 API 的步骤!


<<:  Day22 又回到最初的起点 呆呆地看着萤幕前

>>:  Day8 主动情蒐-情蒐流程、工具与漏洞简介

【Day13】漏洞分析Vulnerability Analysis(二)

哈罗~ 昨天安装完了Window版的Nessus, 今天来做一个简单的扫描实作。 首先先打开浏览器进...

自学教材选择

Photo by The Climate Reality Project on Unsplash ...

[Day 02] 建立开发环境,做好行前准备

老套说:「工欲善其事,必先利其器」— 要写网页,就不能没有好用的开发环境。在开始认识各种前端技能之前...

【资料结构】二元树的删除

说明 说明 1.根结点中的两边固定一边大另一边小。 2.下方节点当作新的根结点,继续符合一边大一边小...

Day30

跟熊熊的课程学习也告一段落了,草莓也发现自己要学的还有很多,同时觉得这一个月的自己变得很不ㄧ样,原来...