<?php
error_reporting(E_ALL & ~E_NOTICE);
// inizializazzione file
include 'token.php';
include 'function.php';
//url preliminare
$url = "https://api.telegram.org/bot" . $token;
// get content of POST request
$content = file_get_contents("php://input");
$update = json_decode($content,true);
// variabili
$chat = $update["message"]["chat"]["id"];
$chat_type = $update["message"]["chat"]["type"];
$text = $update["message"]["text"];
$id = $update["message"]["from"]["id"];
$nome = $update["message"]["from"]["first_name"];
$cognome = $update["message"]["from"]["last_name"];
$username = $update["message"]["from"]["username"];
$msg_id = $update["message"]["message_id"];
$data = NULL;
$ent_type = $update["message"]["reply_to_message"]["entities"][0]["type"];
$ent_offset = $update["message"]["reply_to_message"]["entities"][0]["offset"];
$ent_length = $update["message"]["reply_to_message"]["entities"][0]["length"];
$text_reply = $update["message"]["reply_to_message"]["text"];
if (isset($update["callback_query"]["message"]["chat"]["id"])) {
	$chat = $update["callback_query"]["message"]["chat"]["id"];
	$chat_type = $update["callback_query"]["message"]["chat"]["type"];
	$id = $update["callback_query"]["from"]["id"];
	$data = $update["callback_query"]["data"];
	$msg_id = $update["callback_query"]["message"]["message_id"];
	$nome = $update["callback_query"]["from"]["first_name"];
	$cognome = $update["callback_query"]["from"]["last_name"];
	$username = $update["callback_query"]["from"]["username"];
}
//messaggi
$start_msg = "Ciao $nome\ne benvenuto nel per il test delle API telegram\n\nQuesto bot é sviluppato da @lucafano04 in PHP";
$dona_msg="Se vuoi donare si accettano donazioni via paypall a:\n[luca.facchini.it@gmail.com](https://paypal.me/lucafano04)";
//tastiere
$startk=array(
	'inline_keyboard' => array(
		array(
			array('text'=>'TEST FATTURA','callback_data'=>"fattura1")
		),
		array(
			array('text'=>'❤️Dona','callback_data'=>'dona')
		),
	)
);
$backk=array(
	'inline_keyboard' => array(
		array(
			array('text'=>'Go Back','callback_data'=>"start")
		),
	),
);
$prices_test=array(
	array(
		'label' => "First Product",
		'amount' => 100
	),
	array(
		'label' => "Second Product",
		'amount' => 500
		)
);
//parte pvt
if ($chat_type=="private") {
	// comandi
	switch($text){
		case '/start':
			sendMessage($chat,$start_msg,$startk);
			break;
		case '/paytest':
			sendInvoice($chat,"Test Invoice","This is an ivoice from my bot",1,$prices_test);
			break;
		case '/dice':
			$result=sendDice($chat,"🎲");
			$result=json_decode($result,true);
			$val=$result["result"]["dice"]["value"];
			sleep(5);
			sendMessage($chat,"You get a ".$val."!!!",$backk);
			break;
	}
	//callback data
	if($data=="fattura1"){
		editMessageText($chat,$msg_id,"la fattura verrá inviata presto qua sotto",$backk);
		sendInvoice($chat,"Test Fattura","Questa é una fattura dal mio bot",1,$prices_test);
	}
	if ($data=="start") {
		editMessageText($chat,$msg_id,$start_msg,$startk);
	}
	if ($data=="dona") {
		editMessageText($chat,$msg_id,$dona_msg,$backk);
	}
}
//gruppi
if(isset($content)){
	$file = "input.json";
	$f2 = fopen($file, 'w');
	fwrite($f2, json_encode($update,JSON_PRETTY_PRINT));
	fclose($f2);
}
?>