Jamyy's Weblog

利用 Pushbullet、Twitter 做提醒通知

by Jamyy on 八月.14, 2015, under Linux

從 Linux CLI 排程發送訊息到 PushbulletTwitter, 讓行動裝置於指定時間收到提醒訊息

操作環境: CentOS 6.7 x86_64

Pushbullet

取得 API Key

https://www.pushbullet.com/#settings/account → Access Token

發送測試訊息

$ curl --header 'Authorization: Bearer AccessToken' -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "測試", "body": "這是一個測試"}'

發送訊息到指定裝置

$ wget http://stedolan.github.io/jq/download/linux64/jq (32 位元環境改成 linux32)
$ chmod +x jq
$ mv jq ~/bin/
$ curl --header 'Authorization: Bearer AccessToken' -X GET https://api.pushbullet.com/v2/devices > devices.json
$ jq ".devices" devices.json | less

找出目標裝置的 iden

$ curl --header 'Authorization: Bearer AccessToken' -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"device_iden":"目標裝置iden","type":"note","title":"測試","body":"這是一個測試"}'

撰寫 Shell Script

$ vi ~/bin/remind

#!/bin/bash

MSG=$1
TIME=$(date --date "$2" "+%H:%M %Y-%m-%d")
APIKEY="YourAccessTokenHere"
DEVICE="YourDeviceIdentifyIDHere"
TMPFILE=/tmp/remind.tmp

echo curl --header \'Authorization: Bearer ${APIKEY}\' -X POST https://api.pushbullet.com/v2/pushes --header \'Content-Type: application/json\' --data-binary \'{\"device_iden\":\"${DEVICE}\"\,\"type\":\"note\"\,\"title\":\"提醒\"\,\"body\":\"${MSG}\"}\' > $TMPFILE
at -M -f $TMPFILE $TIME
rm $TMPFILE

$ chmod +x ~/bin/remind

執行 Shell Script

$ remind "測試訊息" now
$ remind "和 Vicky 喝下午茶" "tomorrow 3:00pm"

Twitter

安裝 Twitter CLI

# yum install centos-release-SCL
# yum install ruby193 ruby193-ruby-devel
# echo "source /opt/rh/ruby193/enable" | sudo tee -a /etc/profile.d/ruby193.sh
# source /opt/rh/ruby193/enable
# gem install t

建立 Twitter Application

依照這篇的說明, 登入 Twitter Application Management 建立一個 Twitter App, 目的只是取得 Consumer Key (API Key) 和 Consumer Secret (API Secret) 而已
完成所有步驟之後可執行 t update "測試訊息" 測試能否推文

註: 申請 Twitter App 的 Twitter 帳號必須綁定手機號碼

撰寫 Shell Script

$ vi ~/bin/remind

#!/bin/bash

MSG=$1
TIME=$(date --date "$2" "+%H:%M %Y-%m-%d")

echo "source /opt/rh/ruby193/enable; t update \"$MSG\"" | at -M $TIME

$ chmod +x ~/bin/remind

執行 Shell Script

$ remind "測試訊息" now
$ remind "和 Vicky 喝下午茶" "tomorrow 3:00pm"

假設申請 Twitter App 的 Twitter 帳號為 A
於行動裝置使用 Twitter 帳號 B 來 Follow A 帳號
就可以收到以 Twitter 推文發送的通知

若想在電腦同步檢視 Android 行動裝置收到的通知訊息, 可分別於電腦瀏覽器與行動裝置安裝 "Desktop Notifications for Android"



Ref:



:,