@@ -0,0 +1,3177 @@
+# -*- coding: utf-8 -*-%0A%0A#  Licensed under the Apache License, Version 2.0 (the %22License%22); you may%0A#  not use this file except in compliance with the License. You may obtain%0A#  a copy of the License at%0A#%0A#       http://www.apache.org/licenses/LICENSE-2.0%0A#%0A#  Unless required by applicable law or agreed to in writing, software%0A#  distributed under the License is distributed on an %22AS IS%22 BASIS, WITHOUT%0A#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the%0A#  License for the specific language governing permissions and limitations%0A#  under the License.%0A%0Aimport os%0Aimport sys%0Aimport wsgiref.simple_server%0Afrom argparse import ArgumentParser%0A%0Afrom builtins import bytes%0Afrom linebot import (%0A    LineBotApi, WebhookParser%0A)%0Afrom linebot.exceptions import (%0A    InvalidSignatureError%0A)%0Afrom linebot.models import (%0A    MessageEvent, TextMessage, TextSendMessage%0A)%0Afrom linebot.utils import PY3%0A%0A# get channel_secret and channel_access_token from your environment variable%0Achannel_secret = os.getenv('LINE_CHANNEL_SECRET', None)%0Achannel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)%0Aif channel_secret is None:%0A    print('Specify LINE_CHANNEL_SECRET as environment variable.')%0A    sys.exit(1)%0Aif channel_access_token is None:%0A    print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')%0A    sys.exit(1)%0A%0Aline_bot_api = LineBotApi(channel_access_token)%0Aparser = WebhookParser(channel_secret)%0A%0A%0Adef application(environ, start_response):%0A    # check request path%0A    if environ%5B'PATH_INFO'%5D != '/callback':%0A        start_response('404 Not Found', %5B%5D)%0A        return create_body('Not Found')%0A%0A    # check request method%0A    if environ%5B'REQUEST_METHOD'%5D != 'POST':%0A        start_response('405 Method Not Allowed', %5B%5D)%0A        return create_body('Method Not Allowed')%0A%0A    # get X-Line-Signature header value%0A    signature = environ%5B'HTTP_X_LINE_SIGNATURE'%5D%0A%0A    # get request body as text%0A    wsgi_input = environ%5B'wsgi.input'%5D%0A    content_length = int(environ%5B'CONTENT_LENGTH'%5D)%0A    body = wsgi_input.read(content_length).decode('utf-8')%0A%0A    # parse webhook body%0A    try:%0A        events = parser.parse(body, signature)%0A    except InvalidSignatureError:%0A        start_response('400 Bad Request', %5B%5D)%0A        return create_body('Bad Request')%0A%0A    # if event is MessageEvent and message is TextMessage, then echo text%0A    for event in events:%0A        if not isinstance(event, MessageEvent):%0A            continue%0A        if not isinstance(event.message, TextMessage):%0A            continue%0A%0A        line_bot_api.reply_message(%0A            event.reply_token,%0A            TextSendMessage(text=event.message.text)%0A        )%0A%0A    start_response('200 OK', %5B%5D)%0A    return create_body('OK')%0A%0A%0Adef create_body(text):%0A    if PY3:%0A        return %5Bbytes(text, 'utf-8')%5D%0A    else:%0A        return text%0A%0A%0Aif __name__ == '__main__':%0A    arg_parser = ArgumentParser(%0A        usage='Usage: python ' + __file__ + ' %5B--port %3Cport%3E%5D %5B--help%5D'%0A    )%0A    arg_parser.add_argument('-p', '--port', default=8000, help='port')%0A    options = arg_parser.parse_args()%0A%0A    httpd = wsgiref.simple_server.make_server('', options.port, application)%0Ahttpd.serve_forever()%0A

Create app.py