M
M
Mr. Anderson2019-11-07 13:03:46
PHP
Mr. Anderson, 2019-11-07 13:03:46

Why messages are not sent to telegram via php?

I made a small script for sending messages to a telegram group chat.
Because RKN blocks telegrams with us, I use PROXY_HTTP. The open server is locally deployed on the laptop and everything works well. Messages come and everything is fine.
Well, I think it's time to go to the Internet all this. I registered with AWS (Amazon Web Service), created a server, uploaded my php file there and it seems to work, that is, the page does not show any errors. But still nothing comes to telegram. Help please, where to look, where to see what logs? I can't understand anything. I've been tinkering all day.
Code Listing:

<?php
define('TELEGRAM_TOKEN', 'XXXXXX:XXXXXXXXXXXXXXXXX');
define('TELEGRAM_CHATID', '-12343211234321');

#$message = 'Здорова';
$message = $_GET['message'];

message_to_telegram($message);

function message_to_telegram($mess) {
    $ch = curl_init();
    curl_setopt_array(
        $ch,
        array(
            CURLOPT_URL => 'https://api.telegram.org/bot' . TELEGRAM_TOKEN . '/sendMessage',
            CURLOPT_POST => TRUE,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_POSTFIELDS => array(
                'chat_id' => TELEGRAM_CHATID,
                'text' => $mess,
            ),
            CURLOPT_PROXY => 'qwe-qwe.ra:12354',
            CURLOPT_PROXYUSERPWD => 'qwe-qwerty:asdasd',
            CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
            CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
        )
    );
    curl_exec($ch);
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mr. Anderson, 2019-11-12
@roman_tonkoshkurov

Because server over the hill, then, accordingly, no Proxy is needed there and, accordingly, the code fits in 3 lines. I have it like this:

<?php
$token = "XXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXX";
$chat_id = "-XXXXXXXX";

$message = $_GET['message'];
$message_to_telegram = ($message);

$send_to_telegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$message}","r");

A
Anton B, 2019-11-07
@bigton

Add after curl_exec($ch); var_dump(curl_getinfo($ch)); and see what Telegram answers you

A
ArgosX, 2019-11-07
@ArgosX

1) enable error output

error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', '1');

2) is php-curl module installed?

S
seopublic, 2020-06-16
@seopublic

You also send a message to the bot, specifying its token, you need to add the bot itself to this group chat, specify the group id as the chat_id, it will be with a minus.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question