【Day 11】Button Template 应用 part 2

昨天原本要连同 Confirm buttom 一起讲的~,但平均一下每天的文章量,今天再接着讲另一个按钮类型吧!

Confirm Button Template

看中文字就知道我们大概要做的是一个确认按钮。
所以说,既然是确认,那就只会有两个选项。如下

if event.message.type == 'text':
    mtext = event.message.text
    if "嗨" in mtext:
        message.append(
            TemplateSendMessage(
                alt_text='Confirm template',
                template=ConfirmTemplate(
                    text='有看懂吗',
                        actions=[
                        MessageTemplateAction(
                            label='Yes',
                            text='Yes',
                        ),
                        MessageTemplateAction(
                            label='No',
                            text='No',
                        )
                    ]
                )
            )
        )

应该容易看得出差别,就是把 template= 改成 ConfirmTemplate
我们来看看他原本的 json 里面所有的属性

{
  "type": "template",
  "altText": "this is a confirm template",
  "template": {
      "type": "confirm",
      "text": "Are you sure?",
      "actions": [
          {
            "type": "message",
            "label": "Yes",
            "text": "yes"
          },
          {
            "type": "message",
            "label": "No",
            "text": "no"
          }
      ]
  }
}

Confirm Template 就没有所谓的 title(标题)了,他直接就是 text

然後我们上次也有看到,这次有有看到,在 json 里面有一个 part 是这个:

{
  "type": "message",
  "label": "Yes",
  "text": "yes"
}

action的 type 也分成很多种,基本上用 Message 就已足够应付大部分了。
其余的部分:labeltext 都跟昨天讲的一样。

Carousel Button template

这是一个可以横向选择的多排按钮!

先来看看这个 json

{
  "type": "template",
  "altText": "this is a carousel template",
  "template": {
      "type": "carousel",
      "columns": [
          {
            "thumbnailImageUrl": "https://example.com/bot/images/item1.jpg",
            "imageBackgroundColor": "#FFFFFF",
            "title": "this is menu",
            "text": "description",
            "defaultAction": {
                "type": "uri",
                "label": "View detail",
                "uri": "http://example.com/page/123"
            },
            "actions": [
                {
                    "type": "postback",
                    "label": "Buy",
                    "data": "action=buy&itemid=111"
                },
                {
                    "type": "postback",
                    "label": "Add to cart",
                    "data": "action=add&itemid=111"
                },
                {
                    "type": "uri",
                    "label": "View detail",
                    "uri": "http://example.com/page/111"
                }
            ]
          },
          {
            "thumbnailImageUrl": "https://example.com/bot/images/item2.jpg",
            "imageBackgroundColor": "#000000",
            "title": "this is menu",
            "text": "description",
            "defaultAction": {
                "type": "uri",
                "label": "View detail",
                "uri": "http://example.com/page/222"
            },
            "actions": [
                {
                    "type": "postback",
                    "label": "Buy",
                    "data": "action=buy&itemid=222"
                },
                {
                    "type": "postback",
                    "label": "Add to cart",
                    "data": "action=add&itemid=222"
                },
                {
                    "type": "uri",
                    "label": "View detail",
                    "uri": "http://example.com/page/222"
                }
            ]
          }
      ],
      "imageAspectRatio": "rectangle",
      "imageSize": "cover"
  }
}

他可以制作像以下这样的横向移动的 按钮模板

其余方式等等都语一般的按钮模板一样。
但这里要注意的是:横向按钮模板最多10个;按钮选像这里最多只能有3个。


再来 Button 最後我们剩下了 ImageMap 还没讲到,下集待续,如果有甚麽特别想知道的也可以留言~。


<<:  【Day11】特殊性营运流程篇-CRM

>>:  Day10 - 【概念篇】深入OAuth 2.0

Day10:时程安排

规划时间 开发时间 测试及验收 ...

食谱搜寻系统demo

Icebear终於完成简易的食谱搜寻系统啦!!今天就献丑一下啦!! 明天Icebear会整理系统缺点...

Sorting Algorithms

排序演算法在程序中是非常重要的以下会先来介绍三个基本的排序演算法 Bubble sort Inser...

Spring Framework X Kotlin Day 8 REST Clients & OpenAPI

GitHub Repo https://github.com/b2etw/Spring-Kotlin...

DAY30 - 完赛心得与下一步

第一次参加铁人赛,原本以为超前部署,开赛前两个星期就开始准备文章存档 本以为一定妥当的啦,没想到後面...