PHP Telegram bot: sendMessage not including keyboards

209 views Asked by At

I am trying to send the following array (below is shown the output of print_r($this->arguments, true);), via php, to the Telegram Bot API:

Array
(
    [chat_id] => (omitted)
    [text] => Work in progress...
    [reply_markup] => Array
        (
            [keyboard] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [text] => btn
                                )

                        )

                )

        )

)

then I send the request, as follows:

//...
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->arguments));
//...
$result = curl_exec($ch);

but the response from the server is:

{"ok":true,"result":{"message_id":(omitted),"from":{"id":(omitted),"is_bot":true,"first_name":(omitted),"username":(omitted)},"chat":{"id":(omitted),"first_name":(omitted),"type":"private"},"date":(omitted),"text":"Work in progress..."}}

... that means, the message is actually sent (and visible in the chat), but without the keyboard, that is not shown.

Same error with Inline Keyboards, and same changing the number of the inner reply_markup arrays (documentation asks for "array of array", but tried every combination).

Anyone is any clue about that? It seems all ok with my request, it is blowing my mind...

EDIT: whole code, as requested. Attributes for each Method subclass, like sendMessage (see below) are objects that are all like this:

class Message extends Type {

private $message_id;
/* ... */
/* constructor, getters, setters... */
}

... that are set elsewhere, and execute() puts all of them recursively into the arguments array that is then passed to the cURL request.

abstract class Method {
        private $methodUrl = "https://api.telegram.org/bot"; 
        private $method;
        private $arguments = array();
        private $response;

    private function extractFields($container, $refclassmethods = null) {
            $iteratingOnMethod = !is_null($refclassmethods);
            if (is_object($container)) {
                $methods = get_class_methods($container);
                foreach ($methods as $method) {
                    if (is_getter($method) && !is_null($container->$method()) &&
                            (!$iteratingOnMethod || in_array($method, $refclassmethods))
                    ) {
                        $varname = lcfirst(str_replace("get", "", $method));
                        $val = $container->$method();
                        $ret[$varname] = $this->extractFields($val);
                    }
                }
            } elseif (is_array($container)) {
                foreach ($container as $content) {
                    $ret[] = $this->extractFields($content);
                }
            } else /* it is a "final", atomic value (int, string et similia) */ {
                return $container;
            }
            return $ret;
        }
    
    public function execute() {
    
            $refclass = new ReflectionClass($this::class);
            $this->method = $refclass->getShortName();        
            $this->arguments = ($this->extractFields($this, filterRefClassMethods($refclass)));                
            $this->methodUrl .= API_TOKEN . "/" . $this->method . "?";        
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $this->methodUrl);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->arguments));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
            $result = curl_exec($ch); 
    
            curl_close($ch);   
           
        }

function is_getter(string $method): string {
    return str_starts_with($method, "get");
}

function filterRefClassMethods($refclass) {
    $methods = [];
    foreach ($refclass->getMethods() as $method) {
        if ($method->class === $refclass->getName()) {
            $methods[] = $method->name;
        }
    }
    return $methods;
}

sendMessage class:

class sendMessage extends Method {

protected $chat_id;
protected $message_thread_id;
protected $text;
/* ... */
/* getters and setters... */

}
0

There are 0 answers