10-echo.php

1<?php
2// restituisce l'intero messaggio ricevuto da telegram
3
4// questo token si recupera creando il bot con @botfather https://t.me/botfather
5$botToken = "metti_qui_il_tuo_id";
6
7// prima di poter iniziare a lavorare va impostato il webhook come indicato su
8// https://core.telegram.org/bots/api#setwebhook [1]
9// https://xabaras.medium.com/setting-your-telegram-bot-webhook-the-easy-way-c7577b2d6f72
10$sitoApi = "https://api.telegram.org/bot".$botToken;
11
12// il messaggio è nel corpo della richiesta
13// così la leggo tutta in un colpo
14$content = file_get_contents("php://input"); // [2]
15
16// converto il contenuto da JSON ad array PHP
17$ricevuto = json_decode($content, true);
18
19$chatId=$ricevuto['message']['chat']['id']; 
20
21$messaggio = json_encode($ricevuto,JSON_PRETTY_PRINT);
22sendMessage($chatId,$messaggio);
23
24function sendMessage($chatId,$text){
25    // https://core.telegram.org/bots/api#making-requests
26    // https://core.telegram.org/bots/api#available-methods
27    $url=$GLOBALS['sitoApi'] 
28        ."/sendMessage?chat_id=$chatId&parse_mode=HTML&text=" 
29        .urlencode($text); 
30    // per far prima mi disinteresso leggere il contenuto della rispota
31    // ma soltanto che venga inviata la richiesta quindi faccio così
32    file_get_contents($url);
33}
34

1

Usiamo webkook, per un confronto con "long polling" https://grammy.dev/guide/deployment-types.html#introduction

2

https://www.php.net/manual/en/wrappers.php.php