Azure Function 結合 Line Bot 玩出新花樣 |
您所在的位置:网站首页 › 海外护照丢了怎么补办手续流程图片大全集 › Azure Function 結合 Line Bot 玩出新花樣 |
準備工作:
Azure Services Azure Function Blob Storage(或是直接使用 Azure Function 的 Blob 儲存體也可以)![]() ![]() ![]()
Line develop backend 請使用 Line 帳號登入 develop 後台管理界面,用來取得幾個重要的參數![]() ![]() Azure Function 簡介 Serverless 雲服務 我們不需要擔心伺服器設定或虛擬機的修補與擴充 使用 Serverless,Azure 可以預先準備資源。它透過事件觸發,Azure 會提供必要運算資源。服務執行時才產生費用,所以我們只需要付出執行期間的費用。 每百萬次執行 NT$6.011,收費相當便宜/公道。當然也可以選擇 Consumption Plan 免費模式(幾乎所有功能都有開放,但使用上會有些限制。且一旦選定付費模式就無法調整) Azure Function Pricing Azure Function 的應用 Azure Function 是處理資料、整合系統、使用物聯網 (IoT),以及建置簡單 API 和微服務的絕佳解決方案。 需要排程執行,或大量計算的任何工作 Overview 請注意:預設 5 分鐘後沒有任何驅動動靜時,Azure Function 會進入休眠 若選用付費的價格方案,則可以啟用 Always On,延長或阻止 Azure Function 進入休眠 ![]() ---code 1--- public static class Hook { ///TODO: using HttpClient like a singleton instance private static HttpClient _client = HttpClientFactory.Create(); [FunctionName("Hook")] public static async Task Run ( #if DEBUG [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, #else [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, #endif ExecutionContext context, ILogger log) { var body = await new StreamReader(req.Body).ReadToEndAsync(); log.LogInformation($"{body}"); var xline = GetSignature(req, log); var signatureVerify = new Signature().Verify(body, xline, out string bodySignature); if (!signatureVerify) { log.LogError($"Body Signature: {bodySignature}\nX-Line-Signature: {xline}\n簽章驗證無效!"); return new OkObjectResult(new { }); }第 4. 行。.Net core 2.0 後開始支援 HttpClientFactory。關於 HttpClient 的作法,文末有一系列參考文章。 第 8~12 行,以 #if 切換組態,方便測試環境測試,及正式環境部署 第 20~25 行,linebot 實作中很重要的簽章檢查。以避免遭人冒用 Web Hook。需 Linebot Secret Key。
---code 2--- var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddJsonFile("options.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var settings = config.GetSection("Settings").Get();第 1~8 行,設置設定檔。local.setting.json 是 AZF 預設使用的設定檔,因安全性政策,不允許任何修改(即使本地端修改後測試通過,上傳 Azure 之後會被還原)。但是我們可以利用 .Net core 的特性安插新的自訂設定。
---code 3--- var par = new Parser(body); par.Determine(); var type = par.SourceMsgType.HasValue ? par.SourceMsgType.Value.ToString() : string.Empty; switch (type) { case "text": //MessageTypeEnum.text.ToString(): await GoByTextMessage(par); break; case "image": await GoByImageMessage(par, settings); break; default: break; } return new OkObjectResult(new { }); }說明,Linebot 的主程式片段(因 line 官方並未提供 c# 版 SDK,本範例會使用到我自已實作的 linebot SDK,未完整版,大約有 8、9 成的功能可運作。或者可以考慮使用董大偉老師的 SDK)。範例程式針對「文字」「圖片」訊息反應,其餘忽略。 第 9、10 行,收到文字訊息時,linebot 的回應 第 13、14 行,收到圖片訊息時,會將圖片上傳到 Azure Blob Storage 儲存,再呼叫 Computer Vision: OCR API 識別圖片中的文字,並回傳。 詳細的程式碼請參考這裡 執行範例
參考資源 Azure Function debugging Remote Debugging Azure Functions V2 "The breakpoint will not currently be hit. No symbols have been loaded for this document“ Remote Debugging Azure Functions Line API references 董大偉老師的 Linebot 系列文章 Azure Cognitive Services Computer Vision API - v1.0 LUIS 語言理解 LINE Bot 與人工智慧辨識開發實戰 No.18 Chatbot 開發使用Bot Framework V4 HttpClient usage 使用 HttpClientFactory 實作復原 HTTP 要求 Alternative approach to HttpClient in Azure Functions 2.0 - bringing in HttpClientFactory YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE 在 .NET Core 與 .NET Framework 上使用 HttpClientFactory
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |