2018年2月10日 星期六

使用 botframework + botbuilder-linebot-connector + serverless(AWS lambda) + botbuilder-mongodb-storage 連 mongodb cluster 開發 LineBot 入門 2

https://github.com/Wolke/linebot-serverless-MongoDbStorage-LineConnector-typeScript-starter-Kit

我直接解擇這個架構好了

  1. serverless.yml 這個就serverless 的設定,得配合我開在AWS 上的 mongo cluster ,才能讓 aws lambda 存取。http://wolke-codes.blogspot.tw/2018/01/serverless-aws-lambda-aws-mongodb.html
  2. bot/bot.ts bot 的流程... 可以參考我在IT鐵人的不負責任教學:https://ithelp.ithome.com.tw/users/20046160/ironman/1510
  3. handler.js :這個是serverless 的預設的 function 定義的地方。
    'use strict';
    
    var config = require("./conf");//讀取你的設定
    var bot_dailog = require("./bot/bot")//將bot 流程 require 進來。
    var botbuilder_linebot_connector_1 = require("botbuilder-linebot-connector");
    var botbuilder_mongodb_storage_1 = require("botbuilder-mongodb-storage");
    var builder = require('botbuilder');
    var connector = new botbuilder_linebot_connector_1.LineConnector({
      hasPushApi: false,
      // your line
      channelId: process.env.channelId || config.channelId,
      channelSecret: process.env.channelSecret || config.channelSecret,
      channelAccessToken: process.env.channelAccessToken || config.channelAccessToken
    });//產生connector
    
    
    var bot = new builder.UniversalBot(connector) //產生 bot
    
      .set("storage", new botbuilder_mongodb_storage_1.MongoDbStorage({
        DatabaseName: config.DatabaseName,
        collectionName: config.collectionName,
        mongoIp: config.mongoIp,
        mongoPort: config.mongoPort,
        // mongoIp: "ds125578.mlab.com",
        // mongoPort: "255xx",
        // username: config.username,
        // password: config.password
      }));
      bot_dailog.default(bot)//將bot流程套用
    
    
    
    
    module.exports.line = (event, context, callback) => {
      connector.serverlessWebhock(event) //記得加 connector.serverlessWebhock(event) 就可以了。
    const response = { statusCode: 200, body: JSON.stringify({ message: 'Go Serverless v1.0! Your function executed successfully!', input: event, }), }; callback(null, response); // Use this code if you don't use the http event with the LAMBDA-PROXY integration // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); };
     

沒有留言:

張貼留言