У меня есть 2 проблемы с программированием бота телеграммы на PHP.
<?php
$update = file_get_contents('php://input');
$update = json_decode($update, true);
$chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null;
$mess= "here is my text.";
$mess = $mess. "\n";
$mess = $mess. " this is new line".;
send($mess, $chatId);
function send($text,$chat){
if (strpos($text, "\n")){
$text = urlencode($text);
}
$parameters = array(
"chat_id" => $chat,
"text" => $text,
"parse_mode" => "Markdown"
);
api("sendMessage?", $parameters)
}
function api($method,$command){
$token = "******";
$api = "https://api.telegram.org/bot$token/$method";
if (!$curld = curl_init()){
echo $curld;
}
curl_setopt($curld, CURLOPT_VERBOSE, true);
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $command);
curl_setopt($curld, CURLOPT_URL, $api);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curld, CURLOPT_TIMEOUT, 30);
curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE);
$apiRequest = curl_exec($curld);
curl_close($curld);
return $apiRequest;
}
Мой текст в боте телеграммы выглядит так:
"вот+мой+текст.+это+новая+строка."
Можете ли вы помочь мне с этим?
Спасибо
Специальные символы в вашем тексте вызваны вызовом urlencode()
.
Поскольку вы передаете данные как POSTFIELD, нет необходимости использовать urlencode()
Кроме того, были некоторые синтаксические ошибки, такие как лишний .
позади $mess = $mess. " this is new line".;
.
Обновленный скрипт:
<?php
$update = file_get_contents('php://input');
$update = json_decode($update, true);
$chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null;
$mess= "here is my text.";
$mess = $mess. "\n";
$mess = $mess. " this is new line";
send($mess, $chatId);
function send($text,$chat){
$parameters = array(
"chat_id" => $chat,
"text" => $text,
"parse_mode" => "Markdown"
);
api("sendMessage", $parameters);
}
function api($method, $command){
$token = "!!";
$api = "https://api.telegram.org/bot{$token}/{$method}";
if (!$curld = curl_init()){
echo $curld;
}
// curl_setopt($curld, CURLOPT_VERBOSE, true);
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $command);
curl_setopt($curld, CURLOPT_URL, $api);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curld, CURLOPT_TIMEOUT, 30);
curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE);
$apiRequest = curl_exec($curld);
curl_close($curld);
return $apiRequest;
}
@PřemekTomášDoležal Рад, что вы разобрались! Если кто-то ответит на ваш вопрос, пожалуйста, рассмотрите возможность проголосовать или принять ответ. Это показывает более широкому сообществу, что вы нашли решение, и повышает репутацию как отвечающего, так и вас самих.
Рассмотрите возможность использования библиотеки westacks/telebot. Этот код делает то же самое, но с меньшим количеством кода и более понятным
<?php
require 'vendor/autoload.php';
use WeStacks\TeleBot\TeleBot;
// Init bot
$bot = new TeleBot('your bot token');
// Get update from incoming POST request
$update = $bot->handleUpdate();
// Send message
$bot->sendMessage([
'chat_id' => $update->chat()->id,
'text' => "here is my text.\n this is new line",
"parse_mode" => "Markdown"
]);
Аааа, я этого не понял. Точка была там по ошибке, когда я вставил сюда код. Большое спасибо