I have a Python script that summarizes any chunk of text. I want to call the script in the AfterSave hook of some entity let's say "leadhook.php". Is it possible? If yes, how can I do it?
Announcement
Collapse
No announcement yet.
Text summarization in EspoCRM
Collapse
X
-
I haven't tried this code but it's cobbled together from something similar we have. This assumes your script is under espocrm/custom/scripts. Also this is sloppy because you're passing something unsafe on the command line, but you get the idea.
PHP Code:<?php
namespace Espo\Custom\Hooks\Lead;
use Espo\ORM\Entity;
use Espo\Core\ORM\EntityManager;
class LeadHook
{
private EntityManager $entityManager;
public function __construct(private EntityManager $em)
{
$this->entityManager = $em;
}
public function afterSave(Entity $entity, $options): void
{
// use tempnam(), fopen(), fwrite() if you need
// to process a temporary file, otherwise pass in field
$params = $entity->get('fieldToSummarize') ?? "";
$output = null;
$workdir = dirname($_SERVER['CONTEXT_DOCUMENT_ROOT']);
$command = "$workdir/scripts/myScript.py '$params' 2>&1";
$output = shell_exec($command);
$entity->set('summaryField', $output);
$this->entityManager->saveEntity($entity);
}
}
Comment