Skip to content

Instantly share code, notes, and snippets.

@okdtsk
Last active January 12, 2021 13:47
Show Gist options
  • Select an option

  • Save okdtsk/af70f33e2197b759848df8d6a800ce0a to your computer and use it in GitHub Desktop.

Select an option

Save okdtsk/af70f33e2197b759848df8d6a800ce0a to your computer and use it in GitHub Desktop.

Revisions

  1. okdtsk revised this gist Sep 1, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app.py
    Original file line number Diff line number Diff line change
    @@ -36,5 +36,5 @@ def callback():
    def handle_text_message(event):
    line_bot_api.reply_message(
    event.reply_token,
    TextSendMessage(text='Reply to {}'.format(event.message.text))
    TextSendMessage(text='Reply: {}'.format(event.message.text))
    )
  2. okdtsk revised this gist Sep 1, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app.py
    Original file line number Diff line number Diff line change
    @@ -36,5 +36,5 @@ def callback():
    def handle_text_message(event):
    line_bot_api.reply_message(
    event.reply_token,
    TextSendMessage(text=event.message.text)
    TextSendMessage(text='Reply to {}'.format(event.message.text))
    )
  3. okdtsk revised this gist Sep 1, 2017. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -29,9 +29,6 @@ def callback():
    handler.handle(body, signature)
    except InvalidSignatureError as e:
    return Response({'error': e.message}, status_code=400)
    except LineBotApiError as e:
    return Response({'error': e}, status_code=500)

    return Response({'ok': True})


  4. okdtsk revised this gist Sep 1, 2017. 1 changed file with 2 additions and 11 deletions.
    13 changes: 2 additions & 11 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -28,24 +28,15 @@ def callback():
    try:
    handler.handle(body, signature)
    except InvalidSignatureError as e:
    return Response({'ok': False,
    'error': e.message},
    status_code=400)
    return Response({'error': e.message}, status_code=400)
    except LineBotApiError as e:
    return Response({'ok': False,
    'error': 'Failed {}'.format(e)},
    status_code=500)
    return Response({'error': e}, status_code=500)

    return Response({'ok': True})


    @handler.add(MessageEvent, message=TextMessage)
    def handle_text_message(event):
    # Ignore a request for verification
    # (when pressing 'VERIFY' button in developers console)
    if event.reply_token in ('00000000000000000000000000000000',
    'ffffffffffffffffffffffffffffffff'):
    return
    line_bot_api.reply_message(
    event.reply_token,
    TextSendMessage(text=event.message.text)
  5. okdtsk revised this gist Sep 1, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion app.py
    Original file line number Diff line number Diff line change
    @@ -41,9 +41,10 @@ def callback():

    @handler.add(MessageEvent, message=TextMessage)
    def handle_text_message(event):
    # Ignore a request for verification
    # (when pressing 'VERIFY' button in developers console)
    if event.reply_token in ('00000000000000000000000000000000',
    'ffffffffffffffffffffffffffffffff'):
    # Ignore a request for verification (when pressing 'VERIFY' button in developers console)
    return
    line_bot_api.reply_message(
    event.reply_token,
  6. okdtsk revised this gist Sep 1, 2017. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -28,19 +28,24 @@ def callback():
    try:
    handler.handle(body, signature)
    except InvalidSignatureError as e:
    return Response({'ok': False, 'error': e.message}, status_code=400)
    return Response({'ok': False,
    'error': e.message},
    status_code=400)
    except LineBotApiError as e:
    return Response({'ok': False, 'error': 'Failed to call line bot api {}'.format(e)}, status_code=500)
    return Response({'ok': False,
    'error': 'Failed {}'.format(e)},
    status_code=500)

    return Response({'ok': True})


    @handler.add(MessageEvent, message=TextMessage)
    def handle_text_message(event):
    if event.reply_token in ('00000000000000000000000000000000', 'ffffffffffffffffffffffffffffffff'):
    if event.reply_token in ('00000000000000000000000000000000',
    'ffffffffffffffffffffffffffffffff'):
    # Ignore a request for verification (when pressing 'VERIFY' button in developers console)
    return
    line_bot_api.reply_message(
    event.reply_token,
    TextSendMessage(text='Reply to {}'.format(event.message.text))
    TextSendMessage(text=event.message.text)
    )
  7. okdtsk created this gist Sep 1, 2017.
    20 changes: 20 additions & 0 deletions .chalice_config.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    {
    "version": "2.0",
    "app_name": "./line-bot-for-pycon",
    "stages": {
    "dev": {
    "api_gateway_stage": "dev",
    "environment_variables": {
    "LINEBOT_CHANNEL_ACCESS_TOKEN": "blahblahblah",
    "LINEBOT_CHANNEL_SECRET": "blahblahblah"
    }
    },
    "prod": {
    "api_gateway_stage": "prod",
    "environment_variables": {
    "LINEBOT_CHANNEL_ACCESS_TOKEN": "blahblahblah",
    "LINEBOT_CHANNEL_SECRET": "blahblahblah"
    }
    }
    }
    }
    46 changes: 46 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #!/usr/bin/env python

    import os

    from chalice import Chalice
    from chalice import Response

    from linebot import LineBotApi
    from linebot import WebhookHandler

    from linebot.models import MessageEvent
    from linebot.models import TextMessage
    from linebot.models import TextSendMessage

    from linebot.exceptions import InvalidSignatureError
    from linebot.exceptions import LineBotApiError

    app = Chalice(app_name='line-bot')

    line_bot_api = LineBotApi(os.environ['LINEBOT_CHANNEL_ACCESS_TOKEN'])
    handler = WebhookHandler(os.environ['LINEBOT_CHANNEL_SECRET'])


    @app.route('/callback', methods=['POST'])
    def callback():
    signature = app.current_request.headers['X-Line-Signature']
    body = app.current_request.raw_body.decode('utf-8')
    try:
    handler.handle(body, signature)
    except InvalidSignatureError as e:
    return Response({'ok': False, 'error': e.message}, status_code=400)
    except LineBotApiError as e:
    return Response({'ok': False, 'error': 'Failed to call line bot api {}'.format(e)}, status_code=500)

    return Response({'ok': True})


    @handler.add(MessageEvent, message=TextMessage)
    def handle_text_message(event):
    if event.reply_token in ('00000000000000000000000000000000', 'ffffffffffffffffffffffffffffffff'):
    # Ignore a request for verification (when pressing 'VERIFY' button in developers console)
    return
    line_bot_api.reply_message(
    event.reply_token,
    TextSendMessage(text='Reply to {}'.format(event.message.text))
    )