{
 "name": "Telefonski recepcioner: agent se javi, upiše termin i javi tebi",
 "nodes": [
  {
   "name": "Glasovni agent se javi",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "parameters": {
    "httpMethod": "POST",
    "path": "poziv-je-zavrsen",
    "options": {}
   }
  },
  {
   "name": "AI čita razgovor",
   "type": "@n8n/n8n-nodes-langchain.agent",
   "typeVersion": 2.2,
   "position": [
    200,
    100
   ],
   "parameters": {
    "promptType": "define",
    "text": "=Prepis razgovora:\n{{ $json.body.data.transcript.map(t => (t.role === 'agent' ? 'Agent: ' : 'Pozivalac: ') + t.message).join('\\n') }}",
    "options": {
     "systemMessage": "Dobijaš prepis telefonskog razgovora između glasovnog agenta jedne firme i osobe koja je pozvala.\nOdgovori SAMO JSON-om, bez ičega pre i posle:\n{\"ime\": \"<ime osobe koja zove, ili nepoznato>\", \"trazi\": \"<šta traži, najviše šest reči>\", \"adresa\": \"<adresa ako je rečena, inače prazno>\",\n \"dan\": \"<dogovoreni dan u nedelji malim slovima, npr. petak; prazno ako termin nije dogovoren>\", \"sat\": \"<dogovoreno vreme kao HH:MM; prazno ako nije>\"}\nSAMO ono što je u razgovoru rečeno. Termin važi samo ako ga je agent potvrdio."
    }
   }
  },
  {
   "name": "AI model",
   "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
   "typeVersion": 1.2,
   "position": [
    200,
    320
   ],
   "parameters": {
    "model": {
     "__rl": true,
     "value": "gpt-4o",
     "mode": "list",
     "cachedResultName": "gpt-4o"
    },
    "options": {
     "temperature": 0
    }
   }
  },
  {
   "name": "Upiši termin",
   "type": "n8n-nodes-base.googleCalendar",
   "typeVersion": 1.3,
   "position": [
    460,
    200
   ],
   "parameters": {
    "calendar": {
     "__rl": true,
     "value": "primary",
     "mode": "list",
     "cachedResultName": "primary"
    },
    "start": "={{ (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })() ? (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })().toISO() : '' }}",
    "end": "={{ (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })() ? (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })().plus({ hours: 1 }).toISO() : '' }}",
    "additionalFields": {
     "summary": "={{ JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).ime + ' — ' + JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).trazi }}",
     "description": "={{ JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).adresa }}"
    }
   }
  },
  {
   "name": "Upiši poziv",
   "type": "n8n-nodes-base.googleSheets",
   "typeVersion": 4.7,
   "position": [
    660,
    300
   ],
   "parameters": {
    "operation": "append",
    "documentId": {
     "__rl": true,
     "value": "UPIŠI-SVOJ-ID",
     "mode": "id"
    },
    "sheetName": {
     "__rl": true,
     "value": "Pozivi",
     "mode": "name"
    },
    "columns": {
     "mappingMode": "defineBelow",
     "matchingColumns": [],
     "value": {
      "Datum": "={{ $now.setZone('Europe/Belgrade').toFormat('dd.MM.yyyy.') }}",
      "Ime": "={{ JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).ime }}",
      "Traži": "={{ JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).trazi }}",
      "Termin": "={{ ((() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })() ? JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).dan + ' ' + (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })().toFormat('d.M.') + ' u ' + (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })().toFormat('HH:mm') : 'nije dogovoren') }}",
      "Trajanje": "={{ (Math.floor(Number($('Glasovni agent se javi').first().json.body.data.metadata.call_duration_secs || 0) / 60) + ' min ' + (Number($('Glasovni agent se javi').first().json.body.data.metadata.call_duration_secs || 0) % 60) + ' s') }}"
     },
     "schema": [
      {
       "id": "Datum",
       "displayName": "Datum",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "Ime",
       "displayName": "Ime",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "Traži",
       "displayName": "Traži",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "Termin",
       "displayName": "Termin",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "Trajanje",
       "displayName": "Trajanje",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      }
     ]
    },
    "options": {}
   }
  },
  {
   "name": "Javi tebi",
   "type": "n8n-nodes-base.telegram",
   "typeVersion": 1.2,
   "position": [
    860,
    400
   ],
   "parameters": {
    "chatId": "TVOJ-TELEGRAM-ID",
    "text": "={{ 'Agent se javio · razgovor ' + (Math.floor(Number($('Glasovni agent se javi').first().json.body.data.metadata.call_duration_secs || 0) / 60) + ' min ' + (Number($('Glasovni agent se javi').first().json.body.data.metadata.call_duration_secs || 0) % 60) + ' s') + '\\n' + JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).ime + ' — ' + JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).trazi + (JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).adresa ? ', ' + JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).adresa : '') + '\\nTermin: ' + ((() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })() ? JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')).dan + ' ' + (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })().toFormat('d.M.') + ' u ' + (() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })().toFormat('HH:mm') : 'nije dogovoren') + ((() => { const r = JSON.parse($('AI čita razgovor').first().json.output.replace(/```json|```/g, '')); const d = ['ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota', 'nedelja'].indexOf(String(r.dan || '').toLowerCase()) + 1; if (!d || !/^\\d{1,2}:\\d{2}$/.test(r.sat || '')) return null; let t = $now.setZone('Europe/Belgrade').startOf('day'); do { t = t.plus({ days: 1 }); } while (t.weekday !== d); const [h, m] = r.sat.split(':'); return t.set({ hour: Number(h), minute: Number(m) }); })() ? ' · upisan u kalendar.' : '.') }}",
    "additionalFields": {
     "appendAttribution": false
    }
   }
  }
 ],
 "connections": {
  "Glasovni agent se javi": {
   "main": [
    [
     {
      "node": "AI čita razgovor",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "AI čita razgovor": {
   "main": [
    [
     {
      "node": "Upiši termin",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Upiši termin": {
   "main": [
    [
     {
      "node": "Upiši poziv",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Upiši poziv": {
   "main": [
    [
     {
      "node": "Javi tebi",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "AI model": {
   "ai_languageModel": [
    [
     {
      "node": "AI čita razgovor",
      "type": "ai_languageModel",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 }
}
