Skip to content

以 linebot 串連運勢 api 傳送每日運勢給訂閱者

Notifications You must be signed in to change notification settings

yuchitung/good-luck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Good Luck

Concept

linebot 串連運勢 api 傳送每日運勢給訂閱者。
此範例以 GCP serverless 的方式實作。

�good-luck-structure

步驟

建立 Line@ developer trial

建立一個 GCP 專案

建立一個新的 GCP 專案,之後會在裡面用到

  • Cloud Functions
  • Cloud Pub/Sub
  • Firestore
  • Cloud Scheduler

Firestore

開啟 Firestore 服務,建立一個 collection ,之後給 cloud function 存入使用者的 Line ID 和其它資料使用。

建立註冊/取消訂閱的 cloud function

建立一個 cloud function ,觸發條件使用 http

內容帶入

  • reply/main.py
  • reply/requirements.txt

替換參數

  • ACCESS_TOKEN from Line
  • SECRET from Line
  • FIRESTORE_PROJECT_ID from Firestore
  • COLLECTION_NAME from Firestore

建立定期發送運勢的 cloud function

建立一個 cloud function ,觸發條件使用 Cloud Pub/Sub, 選取主題建立新主題,新增一個 Pub/Sub 主題,給它一個名字 TOPIC_NAME
內容帶入

  • daily_publish/main.py
  • daily_publish/requirements.txt

替換參數

  • ACCESS_TOKEN from Line
  • SECRET from Line
  • FIRESTORE_PROJECT_ID from Firestore
  • COLLECTION_NAME from Firestore

Authenticaton

如果你是自己架 server 串 GCP 的服務,需要建立 service account

本範例因為全部都是使用 GCP 的服務,google 會自動幫你處理。Ref.

If your application runs on Compute Engine, Kubernetes Engine, the App Engine flexible environment, or Cloud Functions, you don't need to create your own service account. Compute Engine includes a default service account that is automatically created for you, and you can assign a different service account, per-instance, if needed. When you create a new instance, the instance is automatically enabled to run as the default service account and has a default set of authorization permissions. For more information, see Compute Engine default service account. After you set up a service account, ADC can implicitly find your credentials without any need to change your code, as described in the section above. If you want to specifically use Compute Engine credentials, you can explicitly do so, as shown in the following code example.

Cloud Pub/Sub

確認這裡可以找到剛剛在 Cloud Functions 建立的主題 projects/PROJECT_ID/topics/TOPIC_NAME

Cloud Scheduler

建立一個 job ,目標選 Pub/Sub ,主題填 TOPIC_NAME,並設定發佈週期

串連 Line Bot

把訂閱/取消 cloud function 的 url 設定在 Line bot 的 Webhook URL
坑點:

Webhook URL cannot be verified

驗證不用過也沒關係,但是要確保 https 有通

使用教學

  • 將 Line Bot 加入好友
  • 輸入你的星座來訂閱
  • 輸入 bye 來取消訂閱
  • 在 Cloud Scheduler 設定的時間會收到當日星座運勢

Reference

Using Cloud Scheduler and Pub/Sub to Trigger a Cloud Function
Line Messaging API
horoscope-crawler

About

以 linebot 串連運勢 api 傳送每日運勢給訂閱者

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages