Sprachausgaben über Amazon Alexa TTS

Hallo,

ich habe vor einigen Tagen ein Skript geschrieben, mit dem es möglich ist, individuelle Sprachausgaben über Amazon Alexa zu ermöglichen. Ich habe dazu auch schon ein Video gemacht: Amazon Alexa Text to Speech TTS über Smart Home nutzen - YouTube

Das ganze ist ein PHP Script leider habe ich aber keine Symcon installation mehr. Hat von euch einer lust das mit mir gemeinsam ein Tutorial für ipsymcon zu erstellen?

Liebe Grüße,
Nico Bode

Hi Nico,

du kannst dir aus dem Service-Bereich ja mal die Demo installieren :slight_smile:

paresy

Hört sich spannend an. Kann gerne versuchen zu helfen.

Gruß

Burkhard

Hallo,

ich werde morgen mal die Demo installieren und mal schauen :slight_smile: danke hatte das garnicht auf dem schirm das es eine kostenlose demo gibt mich hatte nur ein bekannter gefragt ob man das auch mit symcon nutzen kann

Ich habe es bei mir mal ausprobiert, aber die Anmeldung mit dem heruntergeladenen Cookie funktioniert bei mir leider nicht.

Habe aber gerade gesehen, dass die aktuelle OpenHab 2.3 Version inzwischen Alexa TTS und eine ganze Menge weiterer Steuerungsmöglichkeiten bietet:

[ul]
[li]use echo device as text to speech from a rule
[/li][li]volume
[/li][li]pause/continue/next track/previous track
[/li][li]connect/disconnect bluetooth devices
[/li][li]start playing tuneIn radio
[/li][li]start playing Amazon Music
[/li][li]control of multi room music
[/li][li]show album art image in sitemap
[/li][li]speak a reminder message
[/li][li]plays an alarm sound
[/li][li]start traffic news
[/li][li]start daily briefing
[/li][li]start weather report
[/li][li]start good morning report
[/li][li]start automation routine
[/li][li]activate multiple configurations of flash briefings
[/li][li]start playing music by providing the voice command as text (Works with all music providers)
[/li][/ul]

Das ganze ist im amazon-echo-control-binding enthalten.

Das wäre schon ein Hammer, wenn es das auch als Modul für IPS geben würde…:loveips:

Gruß

Burkhard

Ein Modul gibt es ja schon Amazon Echo Remote Control nur sind die besagten neuen Funktionen noch nicht drinnen. Ich bin jetzt auch im Urlaub und habe keine Zeit, aber ihr könnt Euch da ja gerne austoben und die Dinge im existenten PHP Modul ergänzen. Ich selber kann mir das erst nach dem Urlaub mal näher anschauen. Grüße aus :it: Italien.

Oh manno! Das ist ja total an mir vorbeigegangen. Da schau ich mal rein.
Ich wünsche dir einen schönen Urlaub!

LG Burkhard

Ich habe das mal ausprobiert sowohl mit Firefox als auch Chrome, es funktioniert bei mir zumindest nicht. Gibt es da irgendwas spezielles zu beachten? Ansonsten könnte das sicher ergänzt werden im vorhandenen Echo Remote Modul, dazu müsste es aber erst mal gepostete PHP Code grundsätzlich funktionieren ;).

Sprachausgabe über Alexa auf das warte ich schon lange daß das funktioniert.

PHP Skript zum probieren wäre super!
Mfg
Maxx

Das ist das was nicob gepostet hat:


<!doctype html>
<html>
   <head>
      <meta charset="utf-8">
      <title>Alexa Text to Speech (TTS) by Lets Smart Home</title>
      <style>
         table, td, th { border: 1px solid black; }
      </style>
   </head>
   <body>
      <?php
         error_reporting(E_ALL);
         ini_set('display_errors', 1);
         
         //Variablen setzen
         $cookies="cookies.txt";
         $basic_url = 'https://alexa.amazon.de';
		 $browser = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0";
		 $user_lang = 'de,nl-BE;q=0.8,en-US;q=0.5,en;q=0.3';
		 

		echo "Your Browser is ".$_SERVER['HTTP_USER_AGENT']."<br>Your User Lang is: ".$_SERVER['HTTP_ACCEPT_LANGUAGE']."<br> When you have any trouble with this script open Alexa.php and replace this two data in the alexa.php -> user_lang and browser <br><br>";
		if(isset($_GET['device_name'])) {
			$device_name = $_GET['device_name'];
		}


		if(!file_exists($cookies)) die($cookies.' is not available in the order please attach and follow our instructions:');

		$cookie_file_ok = 0;
		if ( $check_cookie_file = file($cookies) ) {
			foreach($check_cookie_file as $check_cookie_file_data) {
				if(strpos($check_cookie_file_data,".amazon.de")!==false) {
					$cookie_file_ok = 1;
				} 
			}
		}
		if($cookie_file_ok == 0) die('Your '.$cookies.' does not seem to be okay Please check');
		
		if (!function_exists('curl_init')) {
   		 	echo "Curl is not enabled or installed on your web server at Synlogy: Webstation -> Php Settings -> Edit Php Profile -> choose the checkbox at extensions & curl";
		} 


		function clear_string($str, $how = '_'){
			$search = array("ä", "ö", "ü", "ß", "Ä", "Ö",
                "Ü", "&", "é", "á", "ó",
                " :)", " :D", " :-)", " :P",
                " :O", " ;D", " ;)", " ^^",
                " :|", " :-/", ":)", ":D",
                ":-)", ":P", ":O", ";D", ";)",
                "^^", ":|", ":-/", "(", ")", "[", "]",
                "<", ">", "!", "\"", "§", "$", "%", "&",
                "/", "(", ")", "=", "?", "`", "´", "*", "'",
                "_", ":", ";", "²", "³", "{", "}",
                "\\", "~", "#", "+", ".", ",",
                "=", ":", "=)");

			$replace = array("ae", "oe", "ue", "ss", "Ae", "Oe",
                 "Ue", "und", "e", "a", "o", "", "",
                 "", "", "", "", "", "", "", "", "",
                 "", "", "", "", "", "", "", "", "",
                 "", "", "", "", "", "", "", "", "",
                 "", "", "", "", "", "", "", "", "",
                 "", "", "", "", "", "", "", "", "",
                 "", "", "", "", "", "", "", "", "", "");

			$str = str_replace($search, $replace, $str);
			$str = strtolower(preg_replace("/[^a-zA-Z0-9]+/", trim($how), $str));
			return $str;
}



         // Geräte lesen

         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL,''.$basic_url.'/api/devices-v2/device?cached=false');
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
         curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         $get_devices = curl_exec($ch);
         $get_devices_info = curl_getinfo($ch);
		 curl_close($ch);

         if($get_devices_info['http_code'] == 200) {

			if(strpos($get_devices,"Anmelden")!==false) {
				echo 'Your cookie information has expired or wrong please renew the cookies.txt If this does not help please delete in the browser all cookies then log in again and then you create a new cookies.txt and upload them! When you use Google Chrome and it isn`t work try it with Firefox ';
				echo '<br><br>';
				die(print_r($get_devices_info));

				
			}


          echo "<h1>The following devices are available in your Amazon account</h1>";
         echo "<table><tr><th>Name</th><th>serialNumber</th><th>deviceFamily</th><th>deviceType</th><th>deviceOwnerCustomerId</th><th>HTTP REQUEST URL</th></tr>";
         $user = json_decode($get_devices);

         	foreach($user->devices as $mydata) {
			if($mydata->deviceFamily == 'WHA') continue;
			if($mydata->deviceFamily == 'VOX') continue;
			if($mydata->deviceFamily == 'FIRE_TV') continue;
			if($mydata->deviceFamily == 'TABLET') continue;



				if(isset($device_name) && $device_name == clear_string($mydata->accountName)) {
		 			$serialNumber = $mydata->serialNumber;
		 			$deviceType = $mydata->deviceType;
		 			$deviceOwnerCustomerId = $mydata->deviceOwnerCustomerId;
				}

         ?>
      				<tr>
         				<td><?php echo clear_string($mydata->accountName);?></td>
         				<td><?php echo $mydata->serialNumber;?></td>
         				<td><?php echo $mydata->deviceFamily;?></td>
         				<td><?php echo $mydata->deviceType;?></td>
         				<td><?php echo $mydata->deviceOwnerCustomerId;?></td>
         				<td>http://<?php echo $_SERVER['HTTP_HOST'];?>/<?php echo $_SERVER['SCRIPT_NAME'];?>?device_name=<?php echo clear_string($mydata->accountName);?>&text_tts=here you can add your text</td>

      				</tr>
      	<?php




      			}
      	
		echo "</table>";
		echo "<BR><BR>";
      } else {
			echo "an unknown error has occurred";	
	  }


		if(isset($_GET['device_name'])) {
			$device_name = $_GET['device_name'];
		} else {
			die('No Device Name (device_name) selected!');
		}

		if(isset($_GET['text_tts'])) {
			$text_tts = $_GET['text_tts'];
		} else {
			die('No Text (text_tts) selected!');
		}
		
		if(strlen($text_tts) >= 1000) die('You really should not use more than 1000 characters in your text ');


		// TTS an amazon Senden
	$cookie_amazon_csrf = 0;
	if ( $get_cookies = file($cookies) ) {
		foreach($get_cookies as $data) {
			if(strpos($data,".amazon.de")!==false) {
				if(strpos($data,"csrf")!==false) {
					$explode = explode('csrf',$data);
					$value = (int)str_replace('-','',$explode[1]);
				 	if($value != 0 ) {
						$cookie_amazon_csrf = $explode[1];
					}
				}
			}
		}
	}

	if($cookie_amazon_csrf == 0) die('Your "csrf" cookie is not available! The request can not be processed - your cookie.txt is wrong');

		$headers = [
			'Host: '.str_replace('https://','',$basic_url).'',
			'User-Agent: '.$browser.'',
			'Accept: */*',
			'Accept-Encoding: deflate, gzip',
			'DNT: 1',
			'Connection: keep-alive',
			'Content-Type: application/json; charset=UTF-8',
			'Accept-Language: '.$user_lang.'',
			'Referer: '.$basic_url.'/spa/index.html',
			'Origin: '.$basic_url.'',
			'csrf: '.(int)$cookie_amazon_csrf.'',
			'Cache-Control: no-cache'
		];

		$curl_tts = curl_init();
		curl_setopt($curl_tts, CURLOPT_URL,''.$basic_url.'/api/behaviors/preview');
		curl_setopt($curl_tts, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl_tts, CURLINFO_HEADER_OUT, 1);
		curl_setopt ($curl_tts, CURLOPT_POST, 1);
		curl_setopt($curl_tts, CURLOPT_POSTFIELDS, '{"behaviorId":"PREVIEW","sequenceJson":"{\"@type\":\"com.amazon.alexa.behaviors.model.Sequence\",\"startNode\":{\"@type\":\"com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode\",\"type\":\"Alexa.Speak\",\"operationPayload\":{\"deviceType\":\"'.$deviceType.'\",\"deviceSerialNumber\":\"'.$serialNumber.'\",\"locale\":\"de-DE\",\"customerId\":\"'.$deviceOwnerCustomerId.'\",\"textToSpeak\":\"'.$text_tts.'\"}}}","status":"ENABLED"}');
		curl_setopt($curl_tts, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl_tts, CURLOPT_COOKIEJAR, $cookies);
		curl_setopt($curl_tts, CURLOPT_COOKIEFILE, $cookies);
		curl_setopt($curl_tts, CURLOPT_FOLLOWLOCATION, true);

		$send_tts = curl_exec($curl_tts);
		$status_tts = curl_getinfo($curl_tts);

		if($status_tts['http_code'] == 200) {
			echo "<b>Your Text ".$text_tts." was sent to the following device: Name: ".$device_name.", serialNumber: ".$serialNumber.", deviceType: ".$deviceType.", deviceOwnerCustomerId: ".$deviceOwnerCustomerId."</b> ";
		} else {
			echo "Unfortunately, an error has leaked!";
			print_r($status_tts);
		}


?>
  </body>
</html>

Einfach den Inhalt in eine PHP Datei z.B. alexa.php in einen Unterordner z.B. echotts des webfront/user Verzeichnisses ablegen zusammen mit dem cookies.txt File und dann im Browser aufrufen mit


http://<IPS IP>:3777/user/echotts/alexa.php

Bei mir funktioniert das aber nicht, zumindest nicht auf die Art, wie er dies beschrieben hat. Vielleicht hast Du ja mehr Glück.
Ansonsten ist das zumindest eine Grundlage sich das mal näher anzuschauen, wenn man dies denn nutzten will.

Das Skript funktioniert auf den ersten Blick nur auf einen Mac!
Windows was ich nutze geht wahrscheinlich nicht.

Mfg
Maxx

Zur IP-Symcon Version 5 kommt TTS und andere Dinge, die neue Version des Moduls ist dann aber ausschließlich erst ab IP-Symcon 5 lauffähig, da hier z.B. ein Konfigurator genutzt wird der erst mit IP-Symcon 5 verfügbar ist.
Fall Du schon schon IPS 5 installiert haben solltest kannst Du mir eine PM schreiben dann kann ich Dir Zugang zum Testen der neuen Version geben. Ansonsten musst Du Dich noch ein paar Tage gedulden bis der IPS 5 Release ansteht, dann geht auch die neue Modul Version mit Text to Speech und anderen Funktionen online.

Update mit TTS ab IP-Symcon 5 siehe hier.

Danke für das neue Modul, habe es seit Gestern in Betrieb. Die Sprachausgabe funzt super.
Das hat mir schon lange gefehlt.