python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page
  • 1.申請帳號
  • 2.在Heroku建立app(Flask)
  • 2-1. 先登入Heroku然後在New選擇Create new app
  • 2-2. 建立完成後在Deployment method選擇Heroku Git,所以需要安裝Git相關設定(參考連結[1])
  • Heroku頁面中的語法(建議再裝個小烏龜TortiseGit比較好操作,沒裝就乖乖用cmd):
  • 2-3. 把Line developers頁面中的Channel secret、Channel access token加到app.py中

Was this helpful?

  1. Linebot

Heroku

PreviousLinebotNext圖文選單

Last updated 5 years ago

Was this helpful?

1.申請帳號

line@manager --> massaging api設定 --> 自動回應不取消 不會影響後續api回覆設定

參考連結:

[1]

2.在Heroku建立app(Flask)

2-1. 先登入Heroku然後在New選擇Create new app

2-2. 建立完成後在Deployment method選擇Heroku Git,所以需要安裝Git相關設定(參考連結[1])

Heroku頁面中的語法(建議再裝個小烏龜TortiseGit比較好操作,沒裝就乖乖用cmd):

(1) 先在cmd中切換到新的專案資料夾,再用git init初始化一個Repository

(2) 與heroku app連接(heroku git:remote -a appName)

(3) 把app所需要的檔案放到資料夾中,主要有四個: requirements.txt --> 需要安裝在python環境中的套件(記得加gunicorn),每次push時會檢查有無心套件需要安裝 runtime.txt --> 指定python版本(用最新的比較保險 吧) Procfile --> 指定需要運行的py檔 app.py --> 就是設定restful api(flask)的py檔,檔名必須要跟Procfile內容一樣

(4) 把這些檔案加到git的repository中(git add .),commit後再push到heroku上,每次修改app.y都需要commit+push

可在Heroku網頁中看log,方便找api的bug不然根本黑盒子
https://dashboard.heroku.com/apps/你的app名稱/logs

2-3. 把Line developers頁面中的Channel secret、Channel access token加到app.py中

# coding='utf-8'
# 回音機器人app.py
from flask import Flask,jsonify
from flask import request
from flask import abort
from datetime import datetime

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import *
import requests
import json

app = Flask(__name__)

line_bot_api = LineBotApi('Access token')
handler = WebhookHandler('Channel secret')


@app.route('/callback', methods=['POST'])
def callback():
    # get X-Line-Signature header value
    try:
        signature = request.headers['X-Line-Signature']
    except Exception as ex:
        app.logger.info("X-Line-Signature error")
        status.update({'error-X-Line-Signature':str(ex)})

    try:
        # get request body as text
        body = request.get_data(as_text=True)
        app.logger.info("Request body: " + body)
    except Exception as ex:
        app.logger.info("Request body error")
        status.update({'error-request.get_data':str(ex)})

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

    return 'Hello, World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    # 說啥就回啥
    line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))

    return 0

if __name__ == '__main__':
    app.run()

參考連結:

[1]

[2]

python linebot doc -->

董大大的教材 -->

聞魚的參考連結 -->

http://www.evanlin.com/create-your-line-bot-golang/
https://github.com/twtrubiks/Deploying-Flask-To-Heroku
https://github.com/twtrubiks/line-bot-tutorial
https://pypi.python.org/pypi/line-bot-sdk
https://hackmd.io/s/SJMM4Afpb
https://elements.heroku.com/buttons/abechen/line-bot-python-heroku
https://medium.com/hondtour/line-chatbot-%E9%96%8B%E7%99%BC%E6%8C%87%E5%8D%97-%E4%B8%80-969fd592c08e