Skip to content

Commit 6355317

Browse files
committed
新增 LineBotPushMessage.py 程式碼 # request 套件 請求
1 parent 0569f71 commit 6355317

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

LineBot/LineBotPushMessage.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding:utf-8 -*-
2+
import requests
3+
import sys
4+
5+
6+
class LineBotFunction:
7+
8+
def __init__(self, push_str, webhook_url):
9+
self.push_str = push_str
10+
self.webhook_url = webhook_url
11+
12+
def push_message(self):
13+
"""
14+
取得 self.push_str 參數值來做判斷
15+
如果是None 則不發送請求
16+
17+
如果有字串的話,會針對Line Bot 的 Webhook URL發送請求
18+
並印出輸入的字串
19+
:return:
20+
"""
21+
if self.push_str is None:
22+
print("未輸入字串,不發送請求")
23+
print("使用方式:請在 python3 ~/LineBotPushMessage.py 空一個輸入參數,記得要加上雙引號")
24+
print("範例: python3 LineBotPushMessage.py \"how are you\"")
25+
else:
26+
requests.get(self.webhook_url + self.push_str)
27+
print(self.webhook_url + self.push_str)
28+
29+
30+
31+
if __name__ == "__main__":
32+
33+
"""
34+
程式碼會從這裡開始跑
35+
在執行程式碼時,請執行 python3 LineBotPushMessage.py "how are you"
36+
"how are you" 代表的是Line Bot 會收到什麼字串
37+
"""
38+
39+
Webhook_URL = {你的WebHook_URL} # 請先修改你的Webhook URL,否則你無法發送請求
40+
41+
try:
42+
push_str = sys.argv[1] # 這串代表的是 它會取得 你執行檔案後面 下一個字串
43+
except Exception:
44+
push_str = None # 當如果沒有輸入會等於None 因此在執行時,不會去請求
45+
46+
LineBot = LineBotFunction(push_str, Webhook_URL) # 取得到的push_str與 Webhook_RUL 會給 LineBotFunction class
47+
LineBot.push_message() # 執行 LineBotFunction內的 push_message function

0 commit comments

Comments
 (0)