Skip to content

Commit

Permalink
Merge pull request #62 from jphacks/feature/rich_menu
Browse files Browse the repository at this point in the history
Feature/rich menu
  • Loading branch information
motty-mio2 authored Nov 17, 2023
2 parents 698cccc + d66ebb3 commit 544e702
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/kb_2315/backend/api/endpoints/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@router.get("/")
def get_calendar(shoe_id: int | None) -> PlainTextResponse:
def get_calendar(shoe_id: int | None = None) -> PlainTextResponse:
JST = timezone(timedelta(hours=+9), "JST")

if shoe_id is None:
Expand Down
32 changes: 22 additions & 10 deletions src/kb_2315/backend/api/endpoints/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,30 @@ async def handle_callback(request: Request) -> Literal["OK"]:

if event.type == "postback":
pbdata: str = event.postback.data # type: ignore
shoe_id, session_id = pbdata.split(":")

if crud_session.map_session_to_shoe(UUID(session_id), int(shoe_id)):
notify.line.send_message(
message="選択を保存しました",
send_to_id=return_id,
)
try:
pbheader: str = pbdata.split(":")[0]
except Exception:
pbheader = ""

if pbheader == "shoes_list":
notify.line.shoe_list_carousel()

elif pbheader == "shoes_select":
_, shoe_id, session_id = pbdata.split(":")

if crud_session.map_session_to_shoe(UUID(session_id), int(shoe_id)):
notify.line.send_message(
message="選択を保存しました",
send_to_id=return_id,
)
else:
notify.line.send_message(
message="選択済みです",
send_to_id=return_id,
)
else:
notify.line.send_message(
message="選択済みです",
send_to_id=return_id,
)
pass
else:
# 普通に話しかけらた
pass
Expand Down
4 changes: 2 additions & 2 deletions src/kb_2315/notify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import line
from . import line, line_rich_menu


__all__: list[str] = ["line"]
__all__: list[str] = ["line", "line_rich_menu"]
Binary file added src/kb_2315/notify/image/menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 30 additions & 1 deletion src/kb_2315/notify/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ def send_message(
print(f"Send Message Error:\n{e}")


def shoe_list_carousel(send_to_id: str = conf.line_group_id) -> None:
columns_list: list[CarouselColumn] = []
shoes: list[Shoe] = crud_shoe.search_shoe_by()

for shoe in shoes:
columns_list.append(
CarouselColumn(
text=f"靴 {shoe.name}",
thumbnail_image_url=shoe.image_url,
actions=[
PostbackAction(label=f"{shoe.name} を選ぶ", data=" :"),
],
)
)

carousel_template_message = TemplateSendMessage(
alt_text="乾燥している靴を選んでください", template=CarouselTemplate(columns=columns_list)
)
line_bot_api = LineBotApi(conf.line_channel_access_token)

try:
line_bot_api.push_message(
to=send_to_id,
messages=carousel_template_message,
)
except LineBotApiError as e:
print(f"LineBotApiError: {e}")


def shoe_select_carousel(send_to_id: str = conf.line_group_id, session_id: UUID | None = None) -> None:
columns_list: list[CarouselColumn] = []
shoes: list[Shoe] = crud_shoe.search_shoe_by()
Expand All @@ -40,7 +69,7 @@ def shoe_select_carousel(send_to_id: str = conf.line_group_id, session_id: UUID
text=f"靴 {shoe.name}",
thumbnail_image_url=shoe.image_url,
actions=[
PostbackAction(label=f"{shoe.name} を選ぶ", data=f"{shoe.id}:{session_id}"),
PostbackAction(label=f"{shoe.name} を選ぶ", data=f"shoes_select:{shoe.id}:{session_id}"),
],
)
)
Expand Down
48 changes: 48 additions & 0 deletions src/kb_2315/notify/line_rich_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pathlib import Path

from linebot import LineBotApi
from linebot.models import RichMenu, RichMenuArea, RichMenuBounds, RichMenuSize
from linebot.models.actions import PostbackAction, URIAction

from kb_2315.config import conf


line_bot_api = LineBotApi(conf.line_channel_access_token)


def create_rich_menu() -> None:
rich_menu_to_create = RichMenu(
size=RichMenuSize(width=1200, height=800),
selected=True,
name="richmenu",
chat_bar_text="メニュー",
areas=[
# カレンダー
RichMenuArea(
bounds=RichMenuBounds(x=0, y=0, width=600, height=800),
action=URIAction(
uri="https://larrybolt.github.io/online-ics-feed-viewer/"
+ f"#feed={conf.host_url}/api/calendar/"
+ "%3Fshoe_id%3D1&cors=false&title=My%20Feed&hideinput=false",
label="カレンダーの表示",
),
),
# データ
RichMenuArea(
bounds=RichMenuBounds(x=600, y=0, width=600, height=400),
action=URIAction(uri=f"{conf.host_url}/analyze/", label="データの表示"),
),
# 靴一覧
RichMenuArea(
bounds=RichMenuBounds(x=600, y=400, width=600, height=400),
action=PostbackAction(data="shoes_list:"),
),
],
)

richMenuId = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)

with open(Path(__file__).parent / "image/menu.png", "rb") as f:
line_bot_api.set_rich_menu_image(richMenuId, "image/png", f)

line_bot_api.set_default_rich_menu(richMenuId)
3 changes: 3 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import kb_2315.config as config
from kb_2315.backend.api.router import api_router
from kb_2315.notify import line_rich_menu


conf: config.env = config.read_config(dir=config.root_dir)
Expand All @@ -14,6 +15,8 @@


if __name__ == "__main__":
line_rich_menu.create_rich_menu()

server = uvicorn.Server(
config=uvicorn.Config(
app=app,
Expand Down

0 comments on commit 544e702

Please sign in to comment.