2018年2月25日 星期日

serverless gcp (google cloud functioin)

參考這個。
開新的 project
要下載keyjson 註明在 serverless.yaml裡

sls deploy 
service: serverless-85332

provider:
name: google
runtime: nodejs
project: serverless-85332 //這裡要註意,要和你的project名稱一樣
# the path to the credentials file needs to be absolute
credentials: ~/.gcloud/keyfile.json
後記:
bug:
因為想查 google cloud functions 有沒有台灣,所以下載 gcloud,但是不知道為何,目錄權限得一直自已開啟。


functions:
line_notify_cpc:
handler: handler.line_notify // handler 的命名,gcp不允許的樣子
events:
- http:
method: get
path: line_notify_cpc

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 }); };
     

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

名字超長的一篇教學,
為什麼會搞到這麼長,
就是經驗而已。


  1. botframework: ms 出的一個開放 chat bot 的一個套件,有官方網站 dev.botframework ,但不幸沒有支援日台泰三國愛用的Line。
  2. botbuilder-linebot-connector : 小弟做的一個connector,連上 botframework的 bulider 不需要 dev.botframework 的官網帳號,就可以來開發bot了。
  3. serverless(AWS lambda) :會選用serverless ,純粹是經驗,因為之前開一個 AWS EC2 micro 的機器,差不多10個使用者,同時在和bot溝通時,就有明顯的delay了,故改用serverless架構,來必免這個scale的問題。
  4. botbuilder-mongodb-storage : 小弟做的一個 mongodb storage for botframework 用的,主要是將 bot state 存在 mongo上使用。
  5. mongodb cluster : 這看你自已的選擇,用 mlab 或是 mongodb altas 或是自已架,都是不錯的選擇。不過請注意 line 有replayToken 會失效的問題,建議不要取存時間過長,導致失效而發不出去。
我的選擇是直接 serverless (AWS lambda 東京) + 開AWS mongodb cluster (https://ap-northeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#SecurityGroups:groupId=sg-db38a1a2;sort=groupId )東京,因為Line 的 bot 機房就在東京。






2018年2月9日 星期五

lambda subnet (serverless )設錯 網域 會連不到...



Select the VPC Subnets that Lambda should use to set up your VPC configuration. Format: "subnet-id (cidr-block) | az name-tag".





subnet-44e59e0d (10.0.0.0/19) | ap-northeast-1a Private subnet 1A

2018年2月8日 星期四

linebot + serverless + typescript 開發時的小問題


serverless 的 serverless offline 套件每次都會從重啟 webpack ,如果是typescript就會重新組譯很久,至少超過1秒,導致我的 linebot reply token 失效。

用 handler.js ,不要寫handler.ts

2018年2月6日 星期二

免費 mongo host mlab .com

https://mlab.com/
有500mb
測試好用

要注意 db 的 user,從
database => users 這裡開的才能用const mongoString = 'mongodb://myUserAdmin:xxxx@ds125578.mlab.com:25578/abcxxxx