iT邦幫忙

0

Line bot 執行問題

  • 分享至 

  • xImage

小弟我最近在研究Line bot互動回話,碰到了python的module無法執行,我也安裝過pip install line-bot-sdk==1.18.0,想請問有沒有解決辦法!!
以下是在ANACONDA環境執行的python程式碼,Api跟Webhook都有確認過

from flask import Flask
app = Flask(__name__)

from flask import 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('K8ZtF+BSoO60azEoDeIIr1Hh1c16vGq4MNhMs1cu8eoCkFh6kbE2Od8VQk3Vd+p+YCFOPIU/ie89cm2zXxCU0o5el63tf5e/2gIPrsZinTscVK78Djo+TLA2AZ7n4si/qNk8sCO5Z54Zb1B5ofEA2QdB04t89/1O/w1cDnyilFU=')
handler = WebhookHandler('a8aa8257cddbf2e0847ac9f6a84fafff')

@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 = "我是 Linebot,\n您好!"
            )
            line_bot_api.reply_message(event.reply_token,message)
        except:
            line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))
        
    elif mtext == '@傳送圖片':
        try:
            message = ImageSendMessage(
                original_content_url = "https://i.imgur.com/4QfKuz1.png",
                preview_image_url = "https://i.imgur.com/4QfKuz1.png"
            )
            line_bot_api.reply_message(event.reply_token,message)
        except:
            line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))

    elif mtext == '@傳送貼圖':
        try:
            message = StickerSendMessage(  #貼圖兩個id需查表
                package_id='1',  
                sticker_id='2'
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))

    elif mtext == '@多項傳送':
        try:
            message = [  #串列
                StickerSendMessage(  #傳送貼圖
                    package_id='1',  
                    sticker_id='2'
                ),
                TextSendMessage(  #傳送文字
                    text = "這是 Pizza 圖片!"
                ),
                ImageSendMessage(  #傳送圖片
                    original_content_url = "https://i.imgur.com/4QfKuz1.png",
                    preview_image_url = "https://i.imgur.com/4QfKuz1.png"
                )
            ]
            line_bot_api.reply_message(event.reply_token,message)
        except:
            line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))

    elif mtext == '@傳送位置':
        try:
            message = LocationSendMessage(
                title='101大樓',
                address='台北市信義路五段7號',
                latitude=25.034207,  #緯度
                longitude=121.564590  #經度
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))

    if mtext == '@快速選單':
        try:
            message = TextSendMessage(
                text='請選擇最喜歡的程式語言',
                quick_reply=QuickReply(
                    items=[
                        QuickReplyButton(
                            action=MessageAction(label="Python", text="Python")
                        ),
                        QuickReplyButton(
                            action=MessageAction(label="Java", text="Java")
                        ),
                        QuickReplyButton(
                            action=MessageAction(label="C#", text="C#")
                        ),
                        QuickReplyButton(
                            action=MessageAction(label="Basic", text="Basic")
                        ),
                    ]
                )
            )
            line_bot_api.reply_message(event.reply_token,message)
        except:
            line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))

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

Han iT邦研究生 1 級 ‧ 2022-01-05 00:05:46 檢舉
token 最好收起來哦...
yun1231 iT邦新手 3 級 ‧ 2022-01-05 00:18:10 檢舉
你要不要先看一下你的`site-packages 資料夾有沒有此套件呢?

是在什麼環境下執行的呢?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答