30-tastiera.php

1<?php
2// inserisce e rimuove una tastiera se l'utente scrive
3// [tastiera|rimuovi|unavolta]
4
5$botToken="metti_qui_il_tuo_id";
6$sitoApi="https://api.telegram.org/bot".$botToken;
7$contentuto = file_get_contents("php://input");
8$ricevuto = json_decode($contentuto, true);
9$text=$ricevuto['message']['text'];
10$chatId=$ricevuto['message']['chat']['id'];
11
12error_log("arrivato");
13
14// attenzione: le coordinate da applicazione desktop non vengono fornite
15$tastiera = '[{"text":"posizione","request_location":true}],'.
16            '[{"text":"uno"},{"text":"due"}]';
17
18// per l'inserimento delle tastiere vedi "reply_markup" di sendMessage
19// https://core.telegram.org/bots/api#sendmessage
20switch($text){
21    case "tastiera":
22        $tastierino='&reply_markup={"keyboard":['.$tastiera.'],'.
23            '"resize_keyboard":true}';
24        sendMessage($chatId, "tastiera on", $tastierino);
25        break;
26    case "rimuovi":
27        $tastierino='&reply_markup={"remove_keyboard":true}';
28        sendMessage($chatId, "tastiera off", $tastierino);
29        break;
30    case "unavolta":
31        $tastierino='&reply_markup={"keyboard":['.$tastiera.'],'.
32            '"resize_keyboard":true,"one_time_keyboard":true}';
33        sendMessage($chatId, "tastiera on", $tastierino);
34        break;
35    default:
36        sendMessage($chatId,$text);
37}
38
39// un parametro in più per eventuali altre cose oltre il messaggio di testo
40function sendMessage($chatId,$text,$aggiunte=""){
41    $url=$GLOBALS['sitoApi']
42        ."/sendMessage?chat_id=$chatId&parse_mode=HTML&text="
43        .urlencode($text).$aggiunte;
44    file_get_contents($url);
45}
46