最新消息

[公告2014/05/30] 如有需要將部落格中,任何一篇文章的程式碼使用在商業用途,請與我聯繫。

[公告2015/04/26] Line版的 iInfo程式與投資應用 群組已上線想加入的朋友們,請先查看 "入群須知" 再與我聯繫 Line : aminwhite5168,加入請告知身分與回答 "入群須知" 的問題。

[公告2018/04/22] 台北 Python + Excel VBA 金融資訊爬蟲課程,課程如網頁內容 金融資訊爬蟲班:台北班 Python 金融資訊爬蟲、EXCEL VBA 金融資訊爬蟲

[公告2019/01/08] 請注意:我再次重申,部落格文章的程式碼,是要提供各位參考與學習,一旦網頁改版請自行修改,別要求東要求西要我主動修改,你們用我寫東西賺錢了、交差了,請問有分我一杯羹嗎?既然賺錢沒分我,請問有什麼理由要求我修改,如果沒能力改,就花錢來找我上課。

[公告2019/12/01] 若各位有 Excel VBA 案子開發需求,歡迎與我聯繫,可接案處理。

[公告2020/05/22] 頁面載入速度慢,起因為部分JS來源(alexgorbatchev.com)失效導致頁面載入變慢,目前已做調整,請多見諒。

2020年3月9日 星期一

Python 在 repl.it 建立Line Bot (2) - 加入指令選項

前一篇 Python 在 repl.it 建立Line Bot (1) - 與Line機器人簡易對談 介紹如何在repl.it上建立Lne Bot 簡易對談,在實際使用上Line Bot功能不指簡易對談,接下來加入指令選項讓Line Bot可以依據訂定好的指令完成所需的回覆。
除了文字對談,Line Bot 還可回應圖片、貼圖、影片、聲音、位置、影像圖、模板選單等,更多的詳細的說明可洽 Line Bot官網 查詢。
以前一篇文章的 Line Bot 為基礎,加入圖片、貼圖、影片回應功能,藉由修改「handle_message」函數,添加處理接收到圖片、貼圖、影片的程式碼,如程如下功能。
  1. 圖片指令,回傳家扶娃娃照片。
  2. 貼圖指令,隨機回傳Line的貼圖。
  3. 影片指令,回傳海賊王片段影片。
from flask import Flask, request, abort

from linebot import (
    LineBotApi, WebhookHandler
)

from linebot.exceptions import (
    InvalidSignatureError
)

from linebot.models import *

import os
import numpy as np

app = Flask(__name__)

token = os.environ.get("CHANNEL_ACCESS_TOKEN")
secret= os.environ.get("CHANNEL_SECRET")
line_bot_api = LineBotApi(token)
handler = WebhookHandler(secret)

totalid = {
            1:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,21,100,101,102,103,104,105,106,107,108,109,110,111,
               112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,
               135,136,137,138,139,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,
               419,420,421,422,423,424,425,426,427,428,429,430],
            2:[18,19,20,22,23,24,258,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,48,141,
               142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,
               165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,501,502,503,504,505,506,507,508,
               509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527],
            3:[180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,
               203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,
               226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,
               249,250,251,252,253,254,255,256,257,258,259],
            4:[260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,
               283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,
               306,307,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,
               622,623,624,625,626,627,628,629,630,631,632]
           } 

@app.route('/callback', methods=['POST'])
def callback():
    signature = request.headers['X-Line-Signature']
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + 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):
    msg = event.message.text    
    print(msg)
    if msg == "文字":
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=event.message.text))
    elif msg == "貼圖":
        packageid = np.random.randint(1, 5)
        stkpkgid = totalid[packageid]
        stickerid = stkpkgid[np.random.randint(len(stkpkgid))]
        line_bot_api.reply_message(event.reply_token, StickerSendMessage(package_id=packageid, sticker_id=stickerid))
    elif msg == "圖片":
        line_bot_api.reply_message(event.reply_token,ImageSendMessage(original_content_url='https://image.peoplenews.tw/news/e48b8810-3b75-46dc-8e00-b64945335bef.jpg', preview_image_url='https://image.peoplenews.tw/news/e48b8810-3b75-46dc-8e00-b64945335bef.jpg'))
    elif msg == "影片":
        line_bot_api.reply_message(event.reply_token,VideoSendMessage(original_content_url='https://video.ftpe3-2.fna.fbcdn.net/v/t42.9040-2/87072388_196826851385614_8294431350157475840_n.mp4?_nc_cat=107&_nc_sid=985c63&efg=eyJybHIiOjQ4NCwicmxhIjoxMTE2LCJ2ZW5jb2RlX3RhZyI6InN2ZV9zZCJ9&_nc_ohc=Dno5wcM5hCoAX9wNCXP&rl=484&vabr=269&_nc_ht=video.ftpe3-2.fna&oh=970be2c547d63b58d9eeeddb50798c78&oe=5E6677B1', preview_image_url='https://scontent.ftpe3-2.fna.fbcdn.net/v/t15.5256-10/p200x200/82227458_520753612181601_8138286452606763008_n.jpg?_nc_cat=101&_nc_sid=f2c4d5&_nc_ohc=lsC6y4SY6jEAX91aBTf&_nc_ht=scontent.ftpe3-2.fna&_nc_tp=6&oh=2b331d81175b9e740ca02479e5e21db0&oe=5E926DE0'))
    else:
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=event.message.text))
        
app.run(host='0.0.0.0', port=8080)

執行畫面:

沒有留言:

張貼留言