GithubHelp home page GithubHelp logo

line-chatbot's Introduction

line bot 實作 django

tags: tutorials

https://yuan-line-chatbot.herokuapp.com/callback

註冊為Line Developer

  1. 前往Line Developer註冊成為Line deeveloper
  2. Create new provider
  3. Create a Message API channel
  4. register some thing
  5. 完成後,可以看到如下畫面

line-chat-bot

sudo apt-get install python3-pip
pip3 install line-bot-sdk
sudo apt install python3-django
pip3 install beautifulsoup4
pip3 install requests
from flask import Flask, request, abort

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

app = Flask(__name__)

line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')


@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:
        print("Invalid signature. Please check your channel access token/channel secret.")
        abort(400)

    return 'OK'


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


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

大功告成

  • 執行LINE Bot應用程式(APP)python manage.py runserver

設定LINE Webhook URL

  • 修改Message API-> Webhook settings

  • 修改Message API-> LINE Official Account features ->Auto-reply messages

部署至heroku

curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
heroku login -i
heroku create yuan-line-chatbot[你-APP-的名字]

prepare Procfile, requirement.txt runtme.txt[optional]

  • Procfile
  • 假設我們所執行的檔案是app_core web: gunicorn app_core:app –preload

git init
heroku git:remote -a yuan-line-chatbot
# set git remote heroku to https://git.heroku.com/yuan-line-chatbot.git
# git config  檢查

git add .
# git status 檢查

git config user.email "[email protected]"
git config user.name "Chen Yu-An" 

git commit -m "v1"   
git push heroku master
# remote: Verifying deploy... done.
# To https://git.heroku.com/yuan-line-chatbot.git
#  * [new branch]      master -> master

TODO list

  • 優化 飲料菜單
  • 提供影片功能

ngrok

將本地服務器暴露到互聯網上,這對於開發和測試非常有用 ngrok http http://localhost:8080

dockerfile

docker build -t my-line-chatbot-app . docker run -d -p 8080:8080 my-line-chatbot-app


Google Cloud Runn access Github Dockerfile

line-chatbot's People

Contributors

a920604a avatar timoomii avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.