iT邦幫忙

2022 iThome 鐵人賽

DAY 11
0
自我挑戰組

轉職AI軟體工程師的自我學習分享筆記系列 第 11

Line Bot 互動式同時回傳 "圖片", "文字", "貼圖"

  • 分享至 

  • xImage
  •  

回傳 “圖片” 訊息

先將想用Line Bot 傳送回覆的圖片傳至雲端, 此篇範例所是將圖片傳至 iThome 的發文草稿中 (Markdown語法) 並複製該圖片的網址~
在前一篇**Day 10: Line Bot 互動式訊息回傳功能** 的 linebotFunc1.py 程式碼中, 修改以下內容:

在第 34 行, 增加以下內容 -

elif mtext == '@想看西藥&拿西藥':
    try:
        message = ImageSendMessage(
            original_content_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
            preview_image_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token,
            TextSendMessage(text= 'Sorry~屁桃故障囉!'))

結果如下

https://ithelp.ithome.com.tw/upload/images/20220925/20151681UB3VE1o38X.jpg

回傳 “貼圖” 訊息

可以到 Line Bot 官方貼圖集 查詢你想要的貼圖, 找到你想要的貼圖後, 複製貼圖的 “Package ID” 和 “Sticker ID” ; 並在 linebotFunc1.py 第44 行增加以下程式碼:

elif mtext == '@想看中藥&拿中藥':
    try:
        message = StickerSendMessage(
            package_id = '8522',
            sticker_id = '16581289'
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token,
            TextSendMessage(text= 'Sorry~屁桃故障囉!'))

結果如下圖:
https://ithelp.ithome.com.tw/upload/images/20220925/20151681VbaVonM7oL.jpg

截至目前為止, 示範的 linebotFunc1.py 程式碼如下:

from flask import Flask
app = Flask(__name__)

from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage,TextSendMessage, ImageSendMessage, StickerSendMessage, LocationSendMessage, QuickReply, QuickReplyButton, MessageAction

line_bot_api = LineBotApi('ZuqEf8yqUPeYP0Ekr9Q3tiv0fy5MVa7MUa9m/BuM9e1M3VBHRBEUjdSIz+Fesg+lzzqz05VbZTasXaWpDwcc3sJi10gft4S9cSMHDQ79jAum51T3qUPwrRzLIiugzovRQceNJ4ro67sVmtihY9lUUAdB04t89/1O/w1cDnyilFU=')
handler = WebhookHandler('3b121dfe3eadcd614bd6bd10cb18f0ca')

@app.route("/callback", methods=['POST'])
def callback():
    signature = request.headers['X-Line-Signature']
    body = request.get_data(as_text=True)
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    return 'OK'

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    mtext = event.message.text
    if mtext == '@確診後流程':
        try:
            message = TextSendMessage(
                text = "確診後該怎麼辦?(SOP圖示)"
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,
                TextSendMessage(text= 'Sorry~屁桃故障囉!'))
    elif mtext == '@想看西藥&拿西藥':
        try:
            message = ImageSendMessage(
                original_content_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
                preview_image_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,
                TextSendMessage(text= 'Sorry~屁桃故障囉!'))
    elif mtext == '@想看中藥&拿中藥':
        try:
            message = StickerSendMessage(
                package_id = '8522',
                sticker_id = '16581289'
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,
                TextSendMessage(text= 'Sorry~屁桃故障囉!'))

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

同時回傳 “文字 & 圖片” 訊息

想更改 按了 “@確診後流程” 的圖文選單會出現 文字與圖片 (當然如果你想要傳送 ”貼圖“ 的話也可以在程式碼中的 message [ #串列中增加StickerSendMessage(....) ]

linebotFunc1.py 截至目前的code為:

from flask import Flask
app = Flask(__name__)

from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage,TextSendMessage, ImageSendMessage, StickerSendMessage, LocationSendMessage, QuickReply, QuickReplyButton, MessageAction

line_bot_api = LineBotApi('ZuqEf8yqUPeYP0Ekr9Q3tiv0fy5MVa7MUa9m/BuM9e1M3VBHRBEUjdSIz+Fesg+lzzqz05VbZTasXaWpDwcc3sJi10gft4S9cSMHDQ79jAum51T3qUPwrRzLIiugzovRQceNJ4ro67sVmtihY9lUUAdB04t89/1O/w1cDnyilFU=')
handler = WebhookHandler('3b121dfe3eadcd614bd6bd10cb18f0ca')

@app.route("/callback", methods=['POST'])
def callback():
    signature = request.headers['X-Line-Signature']
    body = request.get_data(as_text=True)
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    return 'OK'

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    mtext = event.message.text
    if mtext == '@確診後流程':
        try:
            message = [
                TextSendMessage( #傳文字
                text = "確診後該怎麼辦?(SOP圖示)"
                ),
                ImageSendMessage( #傳圖片
                    original_content_url="https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
                    preview_image_url="https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
                ),
                TextSendMessage(  # 傳文字
                    text="小叮嚀: \n \n @因為看線上中醫的程序與需要等待的時間較久, 若想要中西藥都一起吃的話, 可以拿到西藥後 (比較快), 打電話請附近中藥行代煮**清冠一號***的水藥或磨成藥粉先吃, 再花時間處理看中醫需申請的證明, 程序等等...(若已有清冠一號後, 看中醫前, 可以參考圖文選單中間下面的**關於屁桃** > 看中/西醫的小訣竅) \n \n @中藥行的代煮清冠一號, 會請顧客到 **維基百科** 截圖中藥材的配方, 並傳至Line 上請他們代煮水藥或磨成藥粉 (水藥和藥粉的差異請點選 圖文選單中間下面的**關於屁桃** > 確診保養相關資訊)"
                )]
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,
                TextSendMessage(text= 'Sorry~屁桃故障囉!'))
    elif mtext == '@想看西藥&拿西藥':
        try:
            message = ImageSendMessage(
                original_content_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg",
                preview_image_url = "https://ithelp.ithome.com.tw/upload/images/20220925/20151681EaMkK6ROvq.jpg"
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,
                TextSendMessage(text= 'Sorry~屁桃故障囉!'))
    elif mtext == '@想看中藥&拿中藥':
        try:
            message = StickerSendMessage(
                package_id = '8522',
                sticker_id = '16581289'
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,
                TextSendMessage(text= 'Sorry~屁桃故障囉!'))

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

結果點選圖文選單中的 “確診後流程” 就可以同時回傳文字和圖片拉~!

https://ithelp.ithome.com.tw/upload/images/20220925/20151681y0ZG25Tnz4.jpg


上一篇
Line Bot 互動式文字訊息回傳
下一篇
Line Bot 建置快速選單 & 回傳快速選單之訊息
系列文
轉職AI軟體工程師的自我學習分享筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言