Make a call with home assistant

I have to much fun with this contest.

I now can make calls using voice assistant

Oke I do a quick dump of the code needed to make a call except the name lookup.
I’m going to be sick Monday till Wednesday and busy stuff after that.

I have a sync from my android phone to nextcloud to my local asterisk database and ldap server. in my asterisk home assistant project. GitHub - Bram-diederik/asterisk-homeassistant-tools: voicemail system, asterisk number recognition, home assistant integration.

You need a sip account added to your voip device that automatically picks up a call.
So not a account in your asterisk dail plan.

the script to make the to from call

#!/usr/bin/php
<?php
if ($argc != 3) {
die("./start_call.php <from> <to>");
}
$from = $argv[1];
$to =  $argv[2];
$name = "Bram Diederik";
$context = "users";

system("/opt/asterisk/phone_hangup.sh $from");
  $call_file_contents = "Channel: Local/$from@$context\n";
  $call_file_contents .= "Context: $context\n";
  $call_file_contents .= "Extension: $to\n";
  $call_file_contents .= "Priority: 1\n";
  $call_file_contents .= "MaxRetries: 0\n";
  $call_file_contents .= "RetryTime: 60\n";
  $call_file_contents .= "WaitTime: 30\n";
  $call_file_contents .= "CallerID: $name\n";
  $call_file_name = "start_call$from$to";
  // Write the call file
  file_put_contents("/tmp/".$call_file_name, $call_file_contents);
  chmod("/tmp/".$call_file_name,0777);
  rename("/tmp/".$call_file_name,"/var/spool/asterisk/outgoing/".$call_file_name); 

?>


hangup a call

#!/bin/bash
asterisk -x "channel request hangup `asterisk -x "core show channels" | grep PJSIP/$1- | cut -d" " -f1`"

my code to search a name. but this requires my whole sascha setup.

#!/usr/bin/php
<?php
if ($argc < 2) {
    echo "Usage: php search_number.php <part_of_name> [phone type]\n";
    exit(1);
}

// Database connection settings
$dir = "/opt/sascha/nextcloud/";
include($dir."config.php");

// Get the part of the name from the command line argument
$partOfName = $argv[1];
if (@$argv[2]) {
  $type = $argv[2];
} else {
  $type = false;
}

try {
    // Create a PDO database connection
    $pdo = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql1 = "SELECT  contacts.full_name
        FROM contacts 
        WHERE contacts.full_name LIKE :name
        UNION
        SELECT alias.full_name
        FROM alias 
        WHERE alias.full_name LIKE :name";

    $stmt1 = $pdo->prepare($sql1);
    $stmt1->bindValue(':name', '%' . $partOfName . '%', PDO::PARAM_STR);
    $stmt1->execute();
    if ($stmt1->rowCount() == 1)
    {
    // SQL query to search for a phone number by part of a name
     if ($type) {
        $sql = "SELECT phone_numbers.phone_number,phone_numbers.phone_type, contacts.full_name,phone_numbers.pref
        FROM phone_numbers
        JOIN contacts ON phone_numbers.contact_id = contacts.id
        WHERE contacts.full_name LIKE :name
        AND phone_numbers.phone_type LIKE :type
        UNION
        SELECT phone_numbers.phone_number,phone_numbers.phone_type, contacts.full_name,phone_numbers.pref
        FROM phone_numbers
        JOIN contacts ON phone_numbers.contact_id = contacts.id
        JOIN alias ON alias.contact_id = contacts.id
        WHERE alias.full_name LIKE :name
        AND phone_numbers.phone_type LIKE :type
        ORDER BY pref DESC
        LIMIT 1"; // Limiting to one phone number

        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':name', '%' . $partOfName . '%', PDO::PARAM_STR);
        $stmt->bindValue(':type', '%' . $type . '%', PDO::PARAM_STR);
        $stmt->execute();
     } else {
        $sql = "SELECT phone_numbers.phone_number,phone_numbers.phone_type, contacts.full_name,phone_numbers.pref
        FROM phone_numbers
        JOIN contacts ON phone_numbers.contact_id = contacts.id
        WHERE contacts.full_name LIKE :name
        UNION 
        SELECT phone_numbers.phone_number,phone_numbers.phone_type, contacts.full_name,phone_numbers.pref
        FROM phone_numbers
        JOIN contacts ON phone_numbers.contact_id = contacts.id
        JOIN alias ON alias.contact_id = contacts.id
        WHERE alias.full_name LIKE :name
        ORDER BY pref DESC
        LIMIT 1"; // Limiting to one phone number
   
        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':name', '%' . $partOfName . '%', PDO::PARAM_STR);
        $stmt->execute();
     }

    // Check if the phone number was found in the database
    if ($stmt->rowCount() > 0) {
        $result = $stmt->fetch();
        $phoneNumber = $result["phone_number"];
        $fullName = $result["full_name"];
        $phoneType = $result["phone_type"];
        $data = array(
          'fullName' => $fullName,
          'phoneNumber' => $phoneNumber,
          'type' => $phoneType
        );

         $jsonData = json_encode($data);

         echo $jsonData;
    } else {
        echo "No phone number found for '$partOfName'\n";
    }
    } else if ($stmt1->rowCount() == 0) {
        echo "No phone number found for '$partOfName'\n";
    } else {
        echo "Multiple results for '$partOfName'\n";
    }

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$pdo = null;
?>

This looks pretty cool. Definitely keen to see how you made it work!
I’d love for HA to be able to have the functionality to call another mobile device with HA installed like a walkie-talkie.

Well it will be in GitHub - Bram-diederik/asterisk-homeassistant-tools: voicemail system, asterisk number recognition, home assistant integration soonish.
But i need to be in a good mood to update the whole repository.

It uses the same functionality as

to a sip account that automaticly picks up the phone and the end point.

1 Like

I haven’t messed with Asterisk in years, so I definitely want to see how you did this.

Sure thing. I will update my repository in due time.

Meanwhile if you like it you can make a vote.

2 Likes

You can start with Asterisk incomming call assistant
This is a simple addon

2 Likes

I’ll do that, and I’ve voted for this.

Late to the party. My last automation proof concept.
call monitoring

1 Like