ส่งข้อความการ์ดหน้าแรกของแอปสําหรับแอป Google Chat

หน้านี้อธิบายวิธีสร้างและส่งข้อความการ์ดหน้าแรกของแอปสำหรับ แอป Chat หน้าแรกของแอปคือข้อความการ์ดที่ปรับแต่งได้ ซึ่งแอป Chat จะส่งถึงผู้ใช้เมื่อเปิดข้อความส่วนตัวด้วยแอป Chat

การ์ดหน้าแรกของแอปที่มีข้อความ 2 ข้อความ

เช่น คุณสามารถ กำหนดค่าข้อความการ์ดหน้าแรกของแอปเพื่อรวมเคล็ดลับในการโต้ตอบกับแอป Chat โดยใช้ คำสั่งเครื่องหมายทับ สำหรับผู้ใช้ปลายทาง หน้าแรกของแอปจะพร้อมใช้งานในข้อความส่วนตัวของแอป Chat หากนักพัฒนาแอปเปิดใช้ฟีเจอร์นี้


ใช้เครื่องมือสร้างการ์ดเพื่อออกแบบและแสดงตัวอย่างข้อความการ์ด JSON สำหรับแอป Chat ดังนี้

เปิดเครื่องมือสร้างการ์ด

ข้อกำหนดเบื้องต้น

Python

Apps Script

กำหนดค่าในคอนโซล Google Cloud

Python

  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > ผลิตภัณฑ์อื่นๆ > Google Workspace > คลังผลิตภัณฑ์ > Google Chat API

    ไปที่ Google Chat API

  2. คลิก Manage จากนั้นคลิกแท็บ Configuration

  3. เปิดใช้หน้าแรกของแอปการสนับสนุน

  4. เลือกช่องทำเครื่องหมายหน้าแรกของแอปการสนับสนุน

  5. เพิ่ม URL ในช่อง URL หน้าแรกของแอป ค่านี้มักจะเป็น URL เดียวกับ URL ของแอป URL นี้จะถูกเรียกสำหรับ APP_HOME เหตุการณ์

  6. คลิกบันทึก

Apps Script

  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > ผลิตภัณฑ์อื่นๆ > Google Workspace > คลังผลิตภัณฑ์ > Google Chat API

    ไปที่ Google Chat API

  2. คลิก Manage จากนั้นคลิกแท็บ Configuration

  3. เลือกช่องทำเครื่องหมายหน้าแรกของแอปการสนับสนุน

  4. คลิกบันทึก

กำหนดค่าแอป Chat

กำหนดค่าแอป Chat ให้ส่งข้อความการ์ดใหม่สำหรับหน้าแรกของแอป

Python

เมื่อผู้ใช้เปิดข้อความส่วนตัวจากแอป Chat ระบบจะส่งเหตุการณ์ APP_HOME ไปยังแอป Chat เมื่อแอป Chat ได้รับเหตุการณ์นี้ ระบบจะแสดงผลอินสแตนซ์ JSON ของ RenderActions พร้อมการนําทางแบบ pushCard

@app.route('/', methods=['POST'])
def on_event():
  event = request.get_json()
  chat = event.get('chat')
  if chat is not None:
    return handle_chat_event(event)

def handle_chat_event(event):
  if event['chat'].get('type') == 'APP_HOME':
    return get_app_home_card()

def get_app_home_card():
  return {
    "action": {
      "navigations": [
        {
          "pushCard": {
            "sections": [
              {
                "widgets": [
                  {
                    "buttonList": {
                      "buttons": [
                        {
                          "text": "Open documentation",
                          "onClick": {
                            "openLink": {
                              "url": "https://developers.google.com/chat"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  }

Apps Script

ตัวอย่างนี้จะส่งข้อความการ์ดโดยแสดงผล JSON ของการ์ด หรือจะใช้บริการการ์ด Apps Script ก็ได้

ใช้ฟังก์ชัน onAppHome เพื่อแสดงผลอินสแตนซ์ JSON ของ RenderActions ที่มีการนำทาง pushCard ดังนี้

// "onAppHome" is the pre-defined name of the callback that the Chat servers
// execute.
function onAppHome() {
  return {
    action: {
      navigations: [
        {
          pushCard: getCard()
        }
      ]
    }
  };
}

function getCard() {
  return {
    sections: [
      {
        widgets: [
          {
            buttonList: {
              buttons: [
                {
                  text: "Open documentation",
                  onClick: {
                    openLink: {
                      url: "https://developers.google.com/chat"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    ]
  };
}

อัปเดตข้อความการ์ดหน้าแรกของแอป

ข้อความการ์ดหน้าแรกของแอปจะอัปเดตได้เมื่อผู้ใช้ส่งข้อมูลในข้อความของการ์ดหรือปิดกล่องโต้ตอบ เช่น ข้อความเริ่มต้นสำหรับการ์ดหน้าแรกของแอป คือข้อความต้อนรับที่ขอให้ผู้ใช้กรอกแบบฟอร์มที่มีข้อมูล หลังจากที่ผู้ใช้กรอกแบบฟอร์มเสร็จแล้ว ระบบจะส่งข้อความการ์ดหน้าแรกของแอปที่อัปเดตแล้ว การอัปเดตต้องแสดงผลพร้อมกับอินสแตนซ์ของ RenderActions ที่มีการนำทาง updateCard

Python

สำหรับแอป HTTP การอัปเดตข้อความการ์ดหน้าแรกของแอปจะคล้ายกับ การประมวลผลข้อมูลที่ผู้ใช้ป้อน แต่คุณต้องแสดงผล RenderActions invokedFunction แสดงชื่อของฟังก์ชันที่มีการเรียกใช้ซึ่งเชื่อมโยงกับวิดเจ็ต Card ดูข้อมูลเพิ่มเติมได้ที่ CommonEventObject ในตัวอย่างต่อไปนี้ submitForm แสดงให้เห็นว่าผู้ใช้ส่งข้อมูลแบบฟอร์ม

@app.route('/', methods=['POST'])
def on_event():
  event = request.get_json()
  chat = event.get('chat')
  if chat is not None:
    return handle_chat_event(event)

def handle_chat_event(event):
  if event['chat'].get('type') == 'SUBMIT_FORM':
    event_object = event.get('commonEventObject')
    if event_object is not None:
      // Forms
      if 'submitForm' == event_object.get('invokedFunction'):
        return {
          'render_actions': {
            'action': {
              'navigations': [{
                'updateCard': get_update_card()
              }]
            }
          }
        }

def get_update_card():
  return {
      "action": {
          "navigations": [{
              "pushCard": {
                  "sections": [{
                      "widgets": [{
                          "buttonList": {
                              "buttons": [{
                                  "text": "Open documentation",
                                  "onClick": {
                                      "openLink": {
                                          "url": "https://developers.google.com/chat"
                                      }
                                  },
                              }]
                          }
                      }]
                  }]
              }
          }]
      }
  }

Apps Script

ตัวอย่างนี้จะส่งข้อความการ์ดโดยแสดงผล JSON ของการ์ด หรือจะใช้บริการการ์ด Apps Script ก็ได้

// Called from elsewhere (i.e. on button press).
function updateAppHomeCard(event) {
  return {
    render_actions: {
      action: {
        navigations: [
          {
            updateCard: getCard()
          }
        ]
      }
    }
  }
}

function getCard() {
  return {
    sections: [
      {
        widgets: [
          {
            buttonList: {
              buttons: [
                {
                  text: "Open documentation",
                  onClick: {
                    openLink: {
                      url: "https://developers.google.com/chat"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    ]
  };
}

ข้อจำกัด

โดยทั่วไปแล้ว navigation จะใช้กับแอป Chat ไม่ได้ คุณไม่สามารถส่งคืนกลุ่มการ์ดได้ แอป Chat จะใช้ได้เฉพาะ pushCard (สำหรับคำตอบเริ่มต้น) และ updateCard (สำหรับการอัปเดต) เท่านั้น