teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

追加

2018/01/27 03:16

投稿

poker
poker

スコア14

title CHANGED
File without changes
body CHANGED
@@ -39,6 +39,9 @@
39
39
  追記:
40
40
  一部省略しながらですがコードを載せます。
41
41
  ```python
42
+ #!/usr/local/bin/python3.4
43
+  print("Content-type: text/plain\n")
44
+
42
45
  # -*- coding: utf-8 -*-
43
46
 
44
47
  # Licensed under the Apache License, Version 2.0 (the "License"); you may

3

コード追加

2018/01/27 03:16

投稿

poker
poker

スコア14

title CHANGED
File without changes
body CHANGED
@@ -33,4 +33,121 @@
33
33
  "Webhookが無効なHTTPステータスコードを返しました(期待されるステータスコードは200です)"
34
34
  と表示されます。
35
35
 
36
- よろしくお願いします。
36
+ よろしくお願いします。
37
+
38
+
39
+ 追記:
40
+ 一部省略しながらですがコードを載せます。
41
+ ```python
42
+ # -*- coding: utf-8 -*-
43
+
44
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may
45
+ # not use this file except in compliance with the License. You may obtain
46
+ # a copy of the License at
47
+ #
48
+ # http://www.apache.org/licenses/LICENSE-2.0
49
+ #
50
+ # Unless required by applicable law or agreed to in writing, software
51
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
52
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
53
+ # License for the specific language governing permissions and limitations
54
+ # under the License.
55
+
56
+ from __future__ import unicode_literals
57
+
58
+ import errno
59
+ import os
60
+ import sys
61
+ import tempfile
62
+ import requests
63
+ import random
64
+
65
+ from argparse import ArgumentParser
66
+
67
+ from flask import Flask, request, abort
68
+
69
+ from linebot import (
70
+ LineBotApi, WebhookHandler
71
+ )
72
+ from linebot.exceptions import (
73
+ InvalidSignatureError
74
+ )
75
+ from linebot.models import (
76
+ MessageEvent, TextMessage, TextSendMessage,
77
+ SourceUser, SourceGroup, SourceRoom,
78
+ TemplateSendMessage, ConfirmTemplate, MessageTemplateAction,
79
+ ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URITemplateAction,
80
+ PostbackTemplateAction, DatetimePickerTemplateAction,
81
+ CarouselTemplate, CarouselColumn, PostbackEvent,
82
+ StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
83
+ ImageMessage, VideoMessage, AudioMessage, FileMessage,
84
+ UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
85
+ ImageSendMessage
86
+ )
87
+
88
+ import (省略)
89
+
90
+ app = Flask(__name__)
91
+
92
+ # get channel_secret and channel_access_token from your environment variable
93
+ channel_secret = os.getenv('LINE_CHANNEL_SECRET', '(省略)')
94
+ channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', '(省略)')
95
+
96
+ line_bot_api = LineBotApi(channel_access_token)
97
+ handler = WebhookHandler(channel_secret)
98
+
99
+ static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
100
+
101
+ godparent = godparent.godparent()
102
+
103
+ # function for create tmp dir for download content
104
+ def make_static_tmp_dir():
105
+ try:
106
+ os.makedirs(static_tmp_path)
107
+ except OSError as exc:
108
+ if exc.errno == errno.EEXIST and os.path.isdir(static_tmp_path):
109
+ pass
110
+ else:
111
+ raise
112
+
113
+ @app.route("/callback", methods=['POST'])
114
+ def callback():
115
+ # get X-Line-Signature header value
116
+ signature = request.headers['X-Line-Signature']
117
+
118
+ # get request body as text
119
+ body = request.get_data(as_text=True)
120
+ app.logger.info("Request body: " + body)
121
+
122
+ # handle webhook body
123
+ try:
124
+ handler.handle(body, signature)
125
+ except InvalidSignatureError:
126
+ abort(400)
127
+
128
+ return 'OK'
129
+
130
+ @handler.add(MessageEvent, message=TextMessage)
131
+ def handle_text_message(event):
132
+
133
+ response_message = (省略).update(event.message.text)
134
+
135
+ line_bot_api.reply_message(
136
+ event.reply_token, TextSendMessage(response_message))
137
+
138
+ return 0
139
+
140
+ if __name__ == "__main__":
141
+ arg_parser = ArgumentParser(
142
+ usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
143
+ )
144
+ arg_parser.add_argument('-p', '--port', type=int, default=443, help='port')
145
+ arg_parser.add_argument('-d', '--debug', default=False, help='debug')
146
+ options = arg_parser.parse_args()
147
+
148
+ # create tmp dir for download content
149
+ make_static_tmp_dir()
150
+
151
+ app.run(debug=options.debug, port=options.port)
152
+
153
+ ```

2

誤字

2018/01/26 14:12

投稿

poker
poker

スコア14

title CHANGED
File without changes
body CHANGED
@@ -29,6 +29,8 @@
29
29
  https://ドメイン名.ssl-lolipop.jp:443/省略/sources/main.cgi
30
30
  https://ドメイン名.ssl-lolipop.jp:443/省略/sources/main.cgi/callback
31
31
 
32
+ 試しましたが、全て
32
- 試しましたが、全て"Webhookが無効なHTTPステータスコードを返しました(期待されるステータスコードは200です)"と表示されます。
33
+ "Webhookが無効なHTTPステータスコードを返しました(期待されるステータスコードは200です)"
34
+ と表示されます。
33
35
 
34
36
  よろしくお願いします。

1

誤字

2018/01/26 12:50

投稿

poker
poker

スコア14

title CHANGED
File without changes
body CHANGED
@@ -29,8 +29,6 @@
29
29
  https://ドメイン名.ssl-lolipop.jp:443/省略/sources/main.cgi
30
30
  https://ドメイン名.ssl-lolipop.jp:443/省略/sources/main.cgi/callback
31
31
 
32
- 試しましたが、全て
33
- Webhookが無効なHTTPステータスコードを返しました(期待されるステータスコードは200です)
32
+ 試しましたが、全て"Webhookが無効なHTTPステータスコードを返しました(期待されるステータスコードは200です)"と表示されます。
34
- と表示されます。
35
33
 
36
34
  よろしくお願いします。