-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhook.py
More file actions
105 lines (92 loc) · 3.09 KB
/
Webhook.py
File metadata and controls
105 lines (92 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#herokuならコメントアウト
#import config
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
import os
import json
import urllib
app = Flask(__name__)
#環境変数取得(herokuだけ)
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["LINE_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["LINE_CHANNEL_SECRET"]
LINEGROUPID = os.environ["LINEGROUPID"]
LINEID = os.environ["LINEID"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
#ローカル
#YOUR_CHANNEL_ACCESS_TOKEN = config.LINE_CHANNEL_ACCESS_TOKEN
#YOUR_CHANNEL_SECRET = config.LINE_CHANNEL_SECRET
#LINEGROUPID = config.LINEGROUPID
#LINEID = config.LINEID
@app.route("/gitcallback", methods=['POST'])
def gitcallback():
try:
data = json.loads(request.data)
m = len(data['commits']) - 1
s = ''
s = s + str("コミットしたのは: {}".format(data['commits'][m]['author']['name']))
s = s + "\n\n"
s = s + str("コミットメッセージは:\n {}".format(data['commits'][m]['message']))
s = s + "\n\n"
s = s + str("コミットした時間は: {}".format(data['commits'][m]['timestamp']))
s = s + '\n 以上です'
s = s + '\n https://github.com/nagaoyosuke/GithubWebhookTest'
Send(s,LINEGROUPID)
except:
Send('エラーです',LINEID)
return 'OK'
def Send(text,user_id):
url = 'https://api.line.me/v2/bot/message/push'
channel_access_token = YOUR_CHANNEL_ACCESS_TOKEN
# 送信用のデータ
data = {
'to' : user_id,
'messages' : [
{
'type' : 'text',
'text' : text
}
]
}
jsonstr = json.dumps(data).encode('ascii')
print(jsonstr)
# Content-Type:application/json
# Authorization:Bearer {channel access token}
# method:post
request = urllib.request.Request(url, data=jsonstr)
request.add_header('Content-Type', 'application/json')
request.add_header('Authorization', 'Bearer ' + channel_access_token)
request.get_method = lambda: 'POST'
# 送信実行
response = urllib.request.urlopen(request)
ret = response.read()
print('Response:', ret)
'''
def localtest():
with open('test.json') as file:
data = json.load(file)
m = len(data['commits']) - 1
s = ''
s = s + str("コミットしたのは: {}".format(data['commits'][m]['author']['name']))
s = s + "\n\n"
s = s + str("コミットメッセージは:\n {}".format(data['commits'][m]['message']))
s = s + "\n\n"
s = s + str("コミットした時間は: {}".format(data['commits'][m]['timestamp']))
s = s + '\n 以上です'
s = s + '\n https://github.com/nagaoyosuke/GithubWebhookTest'
print(s)
return 'OK'
'''
if __name__ == "__main__":
# app.run()
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)
t()