text: import os
  from pathlib import Path
  from typing import List
  
  from flask import Flask, abort, request
  from linebot import LineBotApi, WebhookHandler
  from linebot.exceptions import InvalidSignatureError
  from linebot.models import (ImageMessage, ImageSendMessage, MessageEvent,
                              TextMessage, TextSendMessage)
  
  from date_the_image import date_the_image
  
  app = Flask(__name__)
  
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
  handler = WebhookHandler(os.environ["CAHNNEL_SECRET"])
  
  SRC_IMAGE_PATH = "static/images/{}.jpg"
  MAIN_IMAGE_PATH = "static/images/{}_main.jpg"
  PREVIEW_IMAGE_PATH = "static/images/{}_preview.jpg"
  
  
  @app.route("/")
  def hello_world():
      return "hello world!"
  
  
  @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:
          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)
      )
  
  
  @handler.add(MessageEvent, message=ImageMessage)
  def handle_image(event):
      message_id = event.message.id
  
      src_image_path = Path(SRC_IMAGE_PATH.format(message_id)).absolute()
      main_image_path = MAIN_IMAGE_PATH.format(message_id)
      preview_image_path = PREVIEW_IMAGE_PATH.format(message_id)
  
      # 画像を保存
      save_image(message_id, src_image_path)
  
      # 画像の加工、保存
      date_the_image(src=src_image_path, desc=Path(main_image_path).absolute())
      date_the_image(src=src_image_path, desc=Path(preview_image_path).absolute())
  
      # 画像の送信
      image_message = ImageSendMessage(
          original_content_url=f"https://date-the-image.herokuapp.com/{main_image_path}",
          preview_image_url=f"https://date-the-image.herokuapp.com/{preview_image_path}",
      )
  
      app.logger.info(f"https://date-the-image.herokuapp.com/{main_image_path}")
      line_bot_api.reply_message(event.reply_token, image_message)
  
      # 画像を削除する
      src_image_path.unlink()
  
  
  def public_attr(obj) -> List[str]:
      return [x for x in obj.__dir__() if not x.startswith("_")]
  
  
  def save_image(message_id: str, save_path: str) -> None:
      """保存"""
      message_content = line_bot_api.get_message_content(message_id)
      with open(save_path, "wb") as f:
          for chunk in message_content.iter_content():
              f.write(chunk)
  
  
  if __name__ == "__main__":
      port = int(os.environ.get("PORT", 5000))
      app.run(host="0.0.0.0", port=port)
  