from flask import Flask, request, abort
import os
import vision as v
import vision1 as v1
import base64
from linebot import (
   LineBotApi, WebhookHandler
)
from linebot.exceptions import (
   InvalidSignatureError
)
from linebot.models import (
   MessageEvent, TextMessage, TextSendMessage, ImageMessage,ButtonsTemplate,TemplateSendMessage,MessageAction #Added ImageMessage, ButtonsTemplate and TemplateSendMessage and MessageAction
)
app = Flask(__name__)
#Get environment variables
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["YOUR_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["YOUR_CHANNEL_SECRET"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
@app.route("/")
def hello_world():
   return "hello world!"
#Specify a URL for the LINE Developers webhook so that the webhook sends an event to the URL
@app.route("/callback", methods=['POST'])
def callback():
   #Get the value for signature verification from the request header
   signature = request.headers['X-Line-Signature']
   #Get request body
   body = request.get_data(as_text=True)
   app.logger.info("Request body: " + body)
   #Validate the signature and call the function defined in handle if there is no problem
   try:
       handler.handle(body, signature)
   except InvalidSignatureError:
       abort(400)
   return 'OK'
#Echolalia for text
# @handler.add(MessageEvent, message=TextMessage)
# def handle_message(event):
#    line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))
#Pattern to get profile and display in rich text
@handler.add(MessageEvent, message=TextMessage)
def response_message(event):
    profile = line_bot_api.get_profile(event.source.user_id)
    status_msg = profile.status_message
    if status_msg != "None":
        #Status registered in LINE_If message is empty,"None"Use the string as an alternative value
        status_msg = "None"
    messages = TemplateSendMessage(alt_text="Buttons template",
                                   template=ButtonsTemplate(
                                       thumbnail_image_url=profile.picture_url,
                                       title=profile.display_name,
                                       text=f"User Id: {profile.user_id[:5]}...\n"
                                            f"Status Message: {status_msg}",
                                       actions=[MessageAction(label="success", text="What should we implement next?")]))
    line_bot_api.reply_message(event.reply_token, messages=messages)
@handler.add(MessageEvent, message=ImageMessage)
def handle_image_message(event):
   push_img_id = event.message.id #Get the posted image ID
   message_content = line_bot_api.get_message_content(push_img_id) #Get images automatically saved on the LINE server
   push_img = b""
   for chunk in message_content.iter_content(): 
       push_img += chunk #Image iter_push with content_Substitute sequentially for img
   push_img = base64.b64encode(push_img).decode("utf-8") #Base64 encoding to pass the API
   msg = v1.recognize_image2(push_img)
   line_bot_api.reply_message(event.reply_token, TextSendMessage(text=msg))
if __name__ == "__main__":
   #    app.run()
   port = int(os.getenv("PORT"))
   app.run(host="0.0.0.0", port=port)
Recommended Posts