Google Chat App webhook not getting direct message

Hi everyone,
I have created a google chat app with appscript and I want to get every message directly from the space without any use @mention.
So far I’ve created a code.gs file which works perfectly fine and gets response and triggers whenever we add to space remove to space and use @mention to send a message no direct message. and i think i have a problem with my appsscript.json file and here is it.

{
  "timeZone": "Asia/Karachi",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/chat.messages.readonly",
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "addOns": {
    "common": {
      "name": "Chat App Quickstart",
      "logoUrl": "https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png"
    },
    "chat": {}
  }
}

This is the file that is giving the response atleast if i use something like this”

{
  "timeZone": "Asia/Karachi",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/chat.bot"
  ],
  "chat": {
    "addToSpaceFallbackMessage": "Thank you for adding me!"
  }
}

with the addOns it don’t even work.if any one want to help i can share the code.gs file too

the code.gs file/**
/**
 * CONFIG
 */
const WEBHOOK_URL = 'https://zap.c4saas.com/webhook/google-chat';

/**
 * Robust text extractor that supports BOTH event shapes:
 * - Add-on events:   event.chat.messagePayload.message.text
 * - Interaction ev:  event.message.text
 */
function getTextFromEvent_(event) {
  try {
    // Interaction-events shape
    if (event && event.message && typeof event.message.text === 'string') {
      return event.message.text;
    }
    // Add-on shape
    const msg = event?.chat?.messagePayload?.message;
    if (msg && typeof msg.text === 'string') {
      return msg.text;
    }
  } catch (e) {}
  return '';
}

/**
 * Extract space/user for both shapes (best-effort).
 */
function getContext_(event) {
  // Interaction-events
  const space = event?.space || event?.chat?.messagePayload?.message?.space
    || event?.chat?.addedToSpacePayload?.space
    || event?.chat?.removedFromSpacePayload?.space
    || null;

  const user  = event?.user || event?.chat?.user || null;

  // Thread (if any)
  const thread = event?.message?.thread
    || event?.chat?.messagePayload?.message?.thread
    || null;

  return { space, user, thread };
}

/**
 * Send to your webhook safely and return a processed string (never throws).
 */
function sendToMake_(kind, event, text) {
  try {
    const { space, user, thread } = getContext_(event);
    const payload = {
      kind,                        // "MESSAGE" | "ADDED_TO_SPACE" | "REMOVED_FROM_SPACE"
      timestamp: new Date().toISOString(),
      user,
      space,
      thread,
      text: text || '',
      raw: event
    };

    const res = UrlFetchApp.fetch(WEBHOOK_URL, {
      method: 'post',
      contentType: 'application/json',
      payload: JSON.stringify(payload),
      muteHttpExceptions: true,
      followRedirects: true
    });

    const code = res.getResponseCode();
    const body = res.getContentText() || '';

    if (code >= 200 && code < 300) {
      try {
        const json = JSON.parse(body);
        if (json && typeof json.processedMessage === 'string' && json.processedMessage) {
          return json.processedMessage;
        }
      } catch (_) {}
      return body || 'OK';
    }
    return `Webhook error ${code}${body ? `: ${body}` : ''}`;
  } catch (err) {
    return 'Error processing message';
  }
}

/**
 * MESSAGE
 * Works for add-on-triggered messages (@mentions) and interaction shape if present.
 */
function onMessage(event) {
  const textIn = getTextFromEvent_(event);
  const processed = sendToMake_('MESSAGE', event, textIn);

  // Try to reply in-thread when available (supports both shapes)
  const { thread } = getContext_(event);
  const response = { text: processed };
  if (thread && thread.name) response.thread = { name: thread.name };
  return response;
}

/**
 * ADDED TO SPACE (new & old names supported)
 */
function onAddedToSpace(event) {
  const { space, user } = getContext_(event);
  const base = (space && space.singleUserBotDm)
    ? `Thank you for adding me to a DM, ${user?.displayName || 'there'}!`
    : `Thank you for adding me to ${space?.displayName || 'this chat'}!`;

  const textIn = getTextFromEvent_(event);
  const suffix = textIn ? ` and you said: "${textIn}"` : '';
  const reply = base + suffix;

  sendToMake_('ADDED_TO_SPACE', event, textIn);
  return { text: reply };
}

/**
 * REMOVED FROM SPACE (new name)
 */
function onRemovedFromSpace(event) {
  sendToMake_('REMOVED_FROM_SPACE', event, '');
  return {};
}

/** Back-compat aliases (if your triggers use the old names) */
function onAddToSpace(e)    { return onAddedToSpace(e); }
function onRemoveFromSpace(e){ return onRemovedFromSpace(e); }

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.