← All recipes

Call before a Google Calendar meeting

Call at a configured offset from a meeting and store a receipt on that event.

Give this recipe to your agent ↗

Prompt

Call at a configured offset from a meeting and store a receipt on that event. Test one source item and its receipt before activation.

Connect the source

Use the provider’s MCP connector in your harness. Inspect its tools and bind the ones needed by the worker. For a direct API integration, the harness puts its credentials in this worker’s account Vault and refreshes them there.

Schedule every five minutes in UTC. Put the source phone in the event’s private extended property phone. Set OFFSET_MINUTES to 15 for a reminder 15 minutes before start, or −15 for a follow-up 15 minutes after start.

Grant event read/write access to the chosen calendar. The poll looks back 24 hours to recover a backlog of at most five calls per run; this can place a late reminder after an outage. The example re-reads current instances so a canceled or moved event drops out. It skips all-day events. The private thoughtly_call property stores the receipt.

Declare these Vault variables: GOOGLE_TOKEN, CALENDAR_ID, OFFSET_MINUTES. Allow these HTTPS hosts: www.googleapis.com. Set on_call_ended: true so this worker receives the phone receipt.

import { createHash, createHmac, timingSafeEqual } from "node:crypto";
export default async function (event, ctx) {
  const env = process.env, enc = encodeURIComponent;
  const raw = Buffer.from(event.body_base64 || "", "base64");
  function equal(value, header) {
    const a = Buffer.from(value), b = Buffer.from(header || "");
    return a.length === b.length && timingSafeEqual(a, b);
  }
  async function json(url, options) {
    const res = await fetch(url, options);
    if (!res.ok) throw new Error("Source request failed (" + res.status + ")");
    return res.status === 204 ? null : res.json();
  }
  const auth = { Authorization: "Bearer " + env.GOOGLE_TOKEN, "Content-Type": "application/json" };
const base = "https://www.googleapis.com/calendar/v3/calendars/" + enc(env.CALENDAR_ID) + "/events";
  if (event.type === "call.ended") {
    const metadata = event.call_metadata || {};
    let id = metadata.source_record || "";
    for (let i = 1; i < 20 && typeof metadata["source_record_" + i] === "string"; i++) id += metadata["source_record_" + i];
    if (!id || !event.data?.call_id) return {skipped:true};
    const receipt = JSON.stringify({call_id:event.data.call_id,status:event.data.status,duration_seconds:event.data.duration_seconds,outcome:event.data.outcome ?? null});
    if (!ctx.dry_run) {
      await json(base + "/" + enc(id),{method:"PATCH",headers:auth,body:JSON.stringify({extendedProperties:{private:{thoughtly_call:receipt}}})});
    }
    return {record_id:id,receipt:JSON.parse(receipt)};
  }
  async function select() {
    if (event.type !== "schedule") return [];
    const offset = Number(env.OFFSET_MINUTES || 0);
const due = Date.parse(event.scheduled_at) + offset * 60000;
const from = new Date(due - 24 * 60 * 60000).toISOString();
// Google treats timeMax as exclusive and ignores fractional seconds.
const to = new Date(due + 1000).toISOString();
    const q = new URLSearchParams({timeMin:from,timeMax:to,singleEvents:"true",showDeleted:"false",maxResults:"2500",orderBy:"startTime"});
    const out = [];
    do { const page = await json(base + "?" + q,{headers:auth});
      for (const e of page.items || []) {
        const fields = e.extendedProperties?.private || {}, start = Date.parse(e.start?.dateTime);
        if (!fields.thoughtly_call && start > Date.parse(from) && start <= due && e.status !== "cancelled") out.push({id:e.id,phone:fields.phone});
      }
      if (!page.nextPageToken) break;
      q.set("pageToken",page.nextPageToken);
    } while (true);
    return out;
  }
  const items = ctx.dry_run && Array.isArray(event.fixture_items) ? event.fixture_items : await select();
  if (!Array.isArray(items)) return items;
  const calls = [], rejected = [];
  for (const {id, phone} of items) {
    if (typeof id !== "string" || !id || id.length > 5120 || !/^\+[1-9]\d{1,14}$/.test(phone || "")) continue;
    const key = "source-record:" + createHash("sha256").update(id).digest("hex");
    const metadata = {source_record:id.slice(0,256)};
    for (let i = 256; i < id.length; i += 256) metadata["source_record_" + (i / 256)] = id.slice(i, i + 256);
    try {
      calls.push(await ctx.calls.place({to:phone,prompt:"Call about the upcoming calendar appointment and ask whether the person can attend.",metadata},key));
    } catch (error) {
      if (error?.status !== 403 || error?.code !== "unsupported_destination") throw error;
      rejected.push({source_key:key, reason:"unsupported_destination"});
      if (rejected.length === 50) break;
    }
    if (calls.length === 5) break;
  }
  return event.type === "webhook" ? {status:200,body:JSON.stringify({count:calls.length,rejected})} : {calls,rejected};
}

Test and activate

Use test_worker with the following event to exercise the call mapping. Worker tests simulate dialing. Then send a real provider event or activate the schedule in a test account and check its run receipt.

{"type":"schedule","fixture_items":[{"id":"lead-42","phone":"+12125550100"}]}

Replay the same source record to check that only one call ID is created. Check the record again after call.ended. The handler uses the original source ID saved in call metadata to write to the correct record. Worker runs are metered, including empty polls. Calls are billed separately. A worker can place up to five calls per run. Unsupported destinations are listed by stable source key in the run result and do not use that five-call limit; the worker stops after 50 such rejections in one run. Fix or remove those source items. Funding, rate-limit and verification failures stop the run so they can be addressed before retrying.

Provider documentation · Worker setup and pricing