2018年7月19日 星期四

line webhook example


webhook.py

#!/bin/python3.6

from flask import Flask, request, abort

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage, ImageSendMessage
)

app = Flask(__name__)

# Channel Access Token
line_bot_api = LineBotApi('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=')
# Channel Secret
handler = WebhookHandler('XXXXXXXXXXXXXXXXXXXXX')

@app.route("/callback", methods=['POST'])
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']

    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " +body)

    # handle webhook body
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)

    return 'OK'



@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    message = TextSendMessage(text=event.message.text+" "+event.source.user_id+" ")
    print(message)
    #line_bot_api.reply_message(event.reply_token, message)

import os
if __name__ == "__main__":
    port = int(os.environ.get('PORT', 8888))
    app.run(host='0.0.0.0', port=port)

more example

https://github.com/line/line-bot-sdk-python

Install Python 3.6.X on CentOS 7


Install Python 3.6.X on CentOS 7

=======================================

yum -y install https://centos7.iuscommunity.org/ius-release.rpm
yum -y install python36
yum -y install python36u-devel
yum -y install python36u-libs
yum -y install python36u-pip

pip3.6  install --upgrade pip
pip3.6  install  line-bot-sdk
pip3.6  install  simplejson
pip3.6  install  responses
pip3.6  install  flask

pip3.6 install pyinstaller

=====================================

Python Flask Test

test.py

#!/bin/python3.6

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello from FLASK"

if __name__ == "__main__":
    app.run(host='127.0.0.1')

======================================

Apache Proxy Setting

proxy.conf

<VirtualHost *:80>
    <Proxy *>
        Order deny,allow
          Allow from all
    </Proxy>
    ProxyPreserveHost On
    <Location "/test">
          ProxyPass "http:///127.0.0.1:5000"
          ProxyPassReverse "http://127.0.0.1:5000"
    </Location>
</VirtualHost>

===========================================

https://www.pyinstaller.org/

PyInstaller Quickstart
Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py


This will generate the bundle in a subdirectory called dist.

For a more detailed walkthrough, see the manual.