LoginSignup
1
1

More than 3 years have passed since last update.

Flutterで作ったアプリから入力した電話番号あてにAWS SNSからSMSの送信をする方法(ざっくり)

Last updated at Posted at 2019-11-29

やりたいこと

入力された電話番号あてにPINを送信したい!

使うもの

AWS
|   DynamoDB
|   API Gateway
|   Lambda
|   L   Node.js 12x
L   SNS
Flutter
L   http 0.12.0+2

DynamoDBの準備

PINコード保存用のテーブルを作成します。

"pin": {
    "user_id":"",
    "pin":"",
    "ttl":"" //TTL設定
}

Lambdaの準備

SMSの送信は publish()を使うことで簡単に送ることができます。

const AWS = require('aws-sdk');
const sns = new AWS.SNS();
const documentClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
    //eventをparseする
    let body = JSON.parse(event.body);
    //電話番号とユーザーIDを取得
    let phone_number = body.phone_number;
    let user_id = body.user_id;

    //PINを生成してデータベースに保存する
    let pin = await getPIN(user_id);
    if(pin == false){
        return false;
    }else{
        //ユーザーにPINを送る
        const phone_number = '+819012345678';// E.164番号の形式 (例: 08012345678 -> +818012345678)
        const message = "あなたの確認コードは${pin}です";
        await sendSMS(phone_number, message); 
    }
}

//PINを生成してDBに保存する
async function genPIN(user_id){
    //今から10分後のエポック秒を生成
    let date = new Date();
    let epoc10min = Math.floor(date.setMinutes(date.getMinutes() + 10) / 1000);

    //PINコードの作成
    let pin = {pinを生成するコード};

    // DBに保存するパラメータ
    let params = {
        'TableName': 'pin',
        'Item': {
            'user_id': user_id,
            'pin': pin,
            'ttl': epoc10min
        }
    };
    try{
        await documentClient.put(params).promise();
        return pin;
    }catch(err){
        console.log(err);
        return false;
    }
}


//SMSを送信する
async function sendSMS(phone_number, message) {

    // ユーザーにPINを早く、確実に送りたいのでSMSの送信タイプをTransactionalにする
    // もし重要度の低い情報であれば、DefaultSMSTypeのValueをPromotionalにする
    const sms_params = {
        attributes: {
            'DefaultSMSType': 'Transactional',
        }
    }
    //送信タイプの設定
    var setSMSTypePromise = new AWS.SNS({
        apiVersion: '2010-03-31'
    }).setSMSAttributes(sms_params).promise();
    setSMSTypePromise.then(
        function (data) {
            console.log(data);
        }).catch(
        function (err) {
            console.error(err, err.stack);
        });

    //送信用パラメータの設定
    var params = {
        Message: message,
        PhoneNumber: phone_number,
    };

    let result;
    // SMS送信
    try {
        await sns.publish(params).promise();
        result = true;
    } catch (e) {
        console.log(e);
        result = false;
    }
    return result;
}

API Gatewayの設定

Lambda統合プロキシでAPI GatewayとLambdaをつないでください。

メソッドはPOSTで。

テスト

Postmanを使ってJSON形式のデータを送ります

{
    "phone_number": "+818012345678",
    "user_id": "hoge"
}

SMSが送信されてきたらOK

Flutter側

このクラスhttp classを使って、作ったAPIあてにPOSTする

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1