Thread View: pl.comp.lang.php
3 messages
3 total messages
Started by mattia.farinaro
Wed, 27 Apr 2016 15:49
php else if
Author: mattia.farinaro
Date: Wed, 27 Apr 2016 15:49
Date: Wed, 27 Apr 2016 15:49
75 lines
3499 bytes
3499 bytes
Ciao a tutti,
Sto facendo un BoT Telegram con comando PHP.
Vorrei che il BoT rispondesse con risposte automatiche solamente se la domanda presenta il "?" alla fine.
Ho creato questo file execute.php ma il BoT mi continua a dare "null" come risposta....ecco il file:
---------------------------------------------------------------------
<?php
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if(!$update)
{
exit;
}
$message = isset($update['message']) ? $update['message'] : "";
$messageId = isset($message['message_id']) ? $message['message_id'] : "";
$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
$senderId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : "";
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : "";
$username = isset($message['chat']['username']) ? $message['chat']['username'] : "";
$date = isset($message['date']) ? $message['date'] : "";
$text = isset($message['text']) ? $message['text'] : "";
$text=trim($text);
$text=strtolower($text);
if (!$text) {
// Verificare che l'ultimo carattere sia un punto di domanda
if (!$text[sizeof($text) - 1] == '?') {
// Da qui mandi la risposta
$answer = $answers[rand(0, sizeof($answers)-1)];
} else {
// Da qui gli mandi "ehy fammi una domanda"
$answer = "Per poterti rispondere, mi devi fare una domanda con un ? alla fine...";
}
}
$answers = array(
"Per quanto posso vedere, sì",
"È certo",
"È decisamente così",
"Molto probabilmente",
"Le prospettive sono buone",
"I segni indicano di sì",
"Senza alcun dubbio",
"Sì",
"Sì",
"Ci puoi contare",
"È difficile rispondere, prova di nuovo",
"Rifai la domanda più tardi",
"Meglio non risponderti adesso",
"Non posso predirlo ora",
"Concentrati e rifai la domanda",
"Non ci contare",
"La mia risposta è no",
"Le mie fonti dicono di no",
"Le prospettive non sono buone",
"Molto incerto"
);
header("Content-Type: application/json");
$parameters = array('chat_id' => $chatId, "text" => $answer);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
---------------------------------------------------------------------
Dove sbaglio???
Grazie in anticipo...
Re: php else if
Author: ww
Date: Fri, 29 Apr 2016 07:52
Date: Fri, 29 Apr 2016 07:52
88 lines
3786 bytes
3786 bytes
W dniu 2016-04-27 o 17:49, mattia.farinaro@gmail.com pisze:
> Ciao a tutti,
> Sto facendo un BoT Telegram con comando PHP.
> Vorrei che il BoT rispondesse con risposte automatiche solamente se la domanda presenta il "?" alla fine.
> Ho creato questo file execute.php ma il BoT mi continua a dare "null" come risposta....ecco il file:
>
> ---------------------------------------------------------------------
>
> <?php
> $content = file_get_contents("php://input");
> $update = json_decode($content, true);
>
> if(!$update)
> {
> exit;
> }
>
> $message = isset($update['message']) ? $update['message'] : "";
> $messageId = isset($message['message_id']) ? $message['message_id'] : "";
> $chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
> $senderId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
> $firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : "";
> $lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : "";
> $username = isset($message['chat']['username']) ? $message['chat']['username'] : "";
> $date = isset($message['date']) ? $message['date'] : "";
> $text = isset($message['text']) ? $message['text'] : "";
>
> $text=trim($text);
>
> $text=strtolower($text);
>
> if (!$text) { // ?????
if (strlen($text)) {
> // Verificare che l'ultimo carattere sia un punto di domanda
> if (!$text[sizeof($text) - 1] == '?') {
if (substr($text, -1) == '?') {
> // Da qui mandi la risposta
$answers = array(
{...}
);
> $answer = $answers[rand(0, sizeof($answers)-1)];
> } else {
> // Da qui gli mandi "ehy fammi una domanda"
> $answer = "Per poterti rispondere, mi devi fare una domanda con un ? alla fine...";
> }
> }
>
> $answers = array(
> "Per quanto posso vedere, sì",
> "È certo",
> "È decisamente così",
> "Molto probabilmente",
> "Le prospettive sono buone",
> "I segni indicano di sì",
> "Senza alcun dubbio",
> "Sì",
> "Sì",
> "Ci puoi contare",
> "È difficile rispondere, prova di nuovo",
> "Rifai la domanda più tardi",
> "Meglio non risponderti adesso",
> "Non posso predirlo ora",
> "Concentrati e rifai la domanda",
> "Non ci contare",
> "La mia risposta è no",
> "Le mie fonti dicono di no",
> "Le prospettive non sono buone",
> "Molto incerto"
> );
>
> header("Content-Type: application/json");
> $parameters = array('chat_id' => $chatId, "text" => $answer);
> $parameters["method"] = "sendMessage";
> echo json_encode($parameters);
>
>
> ---------------------------------------------------------------------
>
> Dove sbaglio???
>
> Grazie in anticipo...
>
Re: php else if
Author: Rafal Podsiadly
Date: Sun, 08 May 2016 07:27
Date: Sun, 08 May 2016 07:27
18 lines
1605 bytes
1605 bytes
Can you use var_dump?
> > <?php
> > $content = file_get_contents("php://input");
> > $update = json_decode($content, true);
var_dump($update)
.......
var_dump($parameters)
> > echo json_encode($parameters);
> >
> >
> > ---------------------------------------------------------------------
> >
> > Dove sbaglio???
> >
> > Grazie in anticipo...
> >
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads