0

Hi guys I'll explain my code:

I have a telegram bot and I have to write many messages quickly in a group and to write a message it is necessary to execute a link (using the Telegram Api).

To do this I use Curl:

curl_setopt($ch, CURLOPT_URL, $url_telegram);
curl_setopt($ch, CURLOPT_HEADER, 0);    
curl_setopt($ch, CURLOPT_VERBOSE, 0);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, getRandomUserAgent());  
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_TIMEOUT, 80);  


$return= curl_exec($ch);        
if($return !== FALSE){  //cURL works
   save_variable_in_js_file();
}

If the message is written I believe it must be:

$return == TRUE or maybe != False

So if is not false is run a function save_variable_in_js_file() that save a variable in a javascript file. The strange thing is that some message are not written by bot, it seems not works and maybe its $return was == FALSE, but anyway the function is run and the variable saved in the file.

Why this ? I know with CURLOPT_RETURNTRANSFER, TRUE $return's curl can be request or false.

Is this the problem ?

The method used with API Telegram to send message is sendMessage: https://core.telegram.org/method/messages.sendMessage


EDIT:

When i write a message with bot, and all is ok, it return this JSON:

ok: true
result: 
  message_id:   546
  from: 
    id: xxx1
    is_bot: true
    first_name: "name bot"
    username:   "name bot"
  chat: 
    id  xxx111
    title   "name group"
    username    "name bot"
    type    "supergroup"
    date    1627749641
    text    "Ciao"

maybe I could do this:

if(json_decode($return, true) ['ok']){
   save_variable_in_js_file();
}

What do you think ?

Borja
  • 3,359
  • 7
  • 33
  • 66
  • Just because it doesn't return `false` doesn't mean that the API call was successful. The cURL request might technically have been successful but the server returned some error message (like 404, validation errors or what ever). Start by adding some logging for what the actual response is (your `$result` variable) and what the response code is. The response is either the boolean `false` (when cURL couldn't make the request) or a string. It won't be the boolean `true`. https://www.php.net/manual/en/function.curl-exec.php – M. Eriksson Jul 31 '21 at 16:30
  • @MagnusEriksson ahhh ok...now understand ! thanks a lot ! Anyway what do you think about my EDIT ? – Borja Jul 31 '21 at 16:47
  • Regarding your edit, that's a good way at least than the previous implementation. – OMi Shah Jul 31 '21 at 16:57

0 Answers0