【问题标题】:Can't send message with russian symbols with Telegram Bot无法使用 Telegram Bot 发送带有俄语符号的消息
【发布时间】:2017-06-07 04:38:14
【问题描述】:

我为电报制作了简单的 php 机器人,它用一条消息回复我。 代码:

<?php
set_time_limit(0);

function SendMessage($message){
    global $website, $chatId;
    file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$message);
}

$botToken = "my token";
$website = "https://api.telegram.org/bot".$botToken;

$content = file_get_contents("php://input");
$update = json_decode($content, TRUE);
$message = $update["message"];

$chatId = $message["chat"]["id"];
$text = $message["text"];

if ($text == "/start") {
    SendMessage("welcome, it's nice to meet you");
} else {
    SendMessage("сори");
}

一切都很好,但在某些时候我的机器人停止了工作。我通过删除所有有效的东西开始调试它。我发现了有趣的结果。

  1. 在我的第一次尝试中,机器人没有发送任何内容。消息同时包含俄语和英语符号。
  2. 第二次我删除了俄语符号,它起作用了。
  3. 但在第三个中,我输入了一个俄语单词,它也起作用了。
  4. 但是当我使用第一次尝试机器人的确切单词时,BUTx2 没有做任何事情。

images of results to make it clear

请向我解释这是怎么可能的。以及如何解决。

更新。我的脚本是用 UTF-8 编码的

【问题讨论】:

  • 你的脚本编码是什么?
  • 编码为 UTF-8
  • 欢迎使用 PHP Unicode 支持
  • 使用这个 $string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
  • 我会尝试应用正确的 URL 编码……

标签: php bots telegram


【解决方案1】:

感谢您的建议。 我通过正确的 URL 编码解决了这个问题。我只是添加了urlencode函数:

function SendMessage($message){
    global $website, $chatId;

    $message = urlencode($message);

    file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$message);
}

【讨论】:

    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 2016-02-23
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多