Php 通过bot向电报通道发送消息

Php 通过bot向电报通道发送消息,php,sendmessage,telegram-bot,Php,Sendmessage,Telegram Bot,我用telegram botfather制作了一个电报机器人,我将我的机器人作为公共电报频道的管理员,现在我想通过机器人在频道中发送消息,这是我的工作代码: php代码是: <?php require('telegram-bot-api.php'); $token = '10**************************************jM'; $bot = new telegram_bot($token); $to = '@myChanne

我用telegram botfather制作了一个电报机器人,我将我的机器人作为公共电报频道的管理员,现在我想通过机器人在频道中发送消息,这是我的工作代码:

php代码是:

<?php
    require('telegram-bot-api.php');

    $token = '10**************************************jM';
    $bot = new telegram_bot($token);
    $to = '@myChannel';
    $rs = $bot->send_message($to , 'test' , null, null);
        print_r($rs);
?>
<?php
class ReplyKeyboardMarkup{
    public $keyboard;
    public $resize_keyboard;
    public $one_time_keyboard;
    public $selective;

    function __construct($resize_keyboard=FALSE, $one_time_keyboard = FALSE, $selective=FALSE){
        $this->keyboard=array();
        $this->keyboard[0]=array();
        $this->resize_keyboard=$resize_keyboard;
        $this->one_time_keyboard=$one_time_keyboard;
        $this->selective=$selective;
    }
    public function add_option($option){
        $this->keyboard = $option;
    }
}
class ReplyKeyboardHide{
    public $hide_keyboard;
    public $selective;

    function __construct($hide_keyboard=TRUE, $selective = FALSE){
        $this->hide_keyboard=$hide_keyboard;
        $this->selective=$selective;
    }
}
class ForceReply{
    public $force_reply;
    public $selective;

    function __construct($force_reply=TRUE, $selective = FALSE){
        $this->force_reply=$force_reply;
        $this->selective=$selective;
    }
}

class telegram_bot{
    private $token;

    private function open_url($url, $method="GET", $data=null){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if($method==="POST"){
            if(isset($data)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

            }
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($ch);
    }

    private function control_api($action, $data=NULL){
        $token = $this->token;
        $response = json_decode($this->open_url("https://api.telegram.org/bot$token$action", "POST", $data));
        return $response;
    }

    function __construct($token){
        $this->token=$token;
    }

    public function status(){
        $response = $this->control_api("/getme");
        return($response);
    }

    public function get_updates(){
        $response = $this->control_api("/getUpdates");
        return($response);
    }

    public function send_action($to, $action){
        $data = array();
        $data["chat_id"]=$to;
        $data["action"]=$action;
        $response = $this->control_api("/sendChatAction", $data);

        return $response;
    }

    public function send_message($to, $msg, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        $data["text"]=$msg;
        $data["disable_web_page_preview"]="true";
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendMessage", $data);

        return $response;
    }

    public function send_location($to, $lat, $lon, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        $data["latitude"]=$lat;
        $data["longitude"]=$lon;
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendLocation", $data);

        return $response;
    }

    public function send_sticker($to, $sticker, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        if(file_exists($sticker))$sticker="@".$sticker;
        $data["sticker"]=$sticker;
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendSticker", $data);

        return $response;
    }

    public function send_video($to, $video, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        if(file_exists($video))$video="@".$video;
        $data["video"]=$video;
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendVideo", $data);
        return $response;
    }

    public function send_photo($to, $photo, $caption, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;

        if(file_exists($photo))$photo="@".$photo;
        $data["photo"]=$photo;
        if(isset($caption)){
            $data["caption"]=$caption;
        }
        if(isset($id_msg)){
            $data["reply_to_message_id"]=$id_msg;
        }
        if(isset($reply))
            $data["reply_markup"]=$reply;

        $response = $this->control_api("/sendPhoto", $data);

        return $response;
    }

    public function send_audio($to, $audio, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;

        if(file_exists($audio))$audio="@".$audio;
        $data["audio"]=$audio;
        if(isset($id_msg)){
            $data["reply_to_message_id"]=$id_msg;
        }
        if(isset($reply))
            $data["reply_markup"]=$reply;

        $response = $this->control_api("/sendAudio", $data);

        return $response;
    }

    public function send_document($to, $document, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;

        if(file_exists($audio))$document="@".$audio;
        $data["document"]=$document;
        if(isset($id_msg)){
            $data["reply_to_message_id"]=$id_msg;
        }
        if(isset($reply))
            $data["reply_markup"]=$reply;

        $response = $this->control_api("/sendDocument", $data);

        return $response;
    }

    public function forward_message($to, $from, $msg_id){
        $data = array();
        $data["chat_id"]=$to;
        $data["from_chat_id"]=$from;
        $data["message_id"]=$msg_id;
        $response = $this->control_api("/forwardMessage", $data);

        return $response;
    }

    public function set_webhook($url=null){
        $data = array();
        $data["url"]=$url;
        $response = $this->control_api("/setWebhook", $data);

        return $response;
    }

    public function get_user_profile_photos($id_user, $offset=null, $limit=null){
        $data = array();
        $data["user_id"]=$id_user;
        if(isset($offset)){
            $data["offset"]=$offset;
        }
        if(isset($limit)){
            $data["limit"]=$limit;
        }

        $response = $this->control_api("/getUserProfilePhotos", $data);

        return $response;
    }

    public function read_post_message(){
        return json_decode(file_get_contents('php://input'));
    }

}
?>

电报-bot-api.php代码为:

<?php
    require('telegram-bot-api.php');

    $token = '10**************************************jM';
    $bot = new telegram_bot($token);
    $to = '@myChannel';
    $rs = $bot->send_message($to , 'test' , null, null);
        print_r($rs);
?>
<?php
class ReplyKeyboardMarkup{
    public $keyboard;
    public $resize_keyboard;
    public $one_time_keyboard;
    public $selective;

    function __construct($resize_keyboard=FALSE, $one_time_keyboard = FALSE, $selective=FALSE){
        $this->keyboard=array();
        $this->keyboard[0]=array();
        $this->resize_keyboard=$resize_keyboard;
        $this->one_time_keyboard=$one_time_keyboard;
        $this->selective=$selective;
    }
    public function add_option($option){
        $this->keyboard = $option;
    }
}
class ReplyKeyboardHide{
    public $hide_keyboard;
    public $selective;

    function __construct($hide_keyboard=TRUE, $selective = FALSE){
        $this->hide_keyboard=$hide_keyboard;
        $this->selective=$selective;
    }
}
class ForceReply{
    public $force_reply;
    public $selective;

    function __construct($force_reply=TRUE, $selective = FALSE){
        $this->force_reply=$force_reply;
        $this->selective=$selective;
    }
}

class telegram_bot{
    private $token;

    private function open_url($url, $method="GET", $data=null){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if($method==="POST"){
            if(isset($data)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

            }
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($ch);
    }

    private function control_api($action, $data=NULL){
        $token = $this->token;
        $response = json_decode($this->open_url("https://api.telegram.org/bot$token$action", "POST", $data));
        return $response;
    }

    function __construct($token){
        $this->token=$token;
    }

    public function status(){
        $response = $this->control_api("/getme");
        return($response);
    }

    public function get_updates(){
        $response = $this->control_api("/getUpdates");
        return($response);
    }

    public function send_action($to, $action){
        $data = array();
        $data["chat_id"]=$to;
        $data["action"]=$action;
        $response = $this->control_api("/sendChatAction", $data);

        return $response;
    }

    public function send_message($to, $msg, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        $data["text"]=$msg;
        $data["disable_web_page_preview"]="true";
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendMessage", $data);

        return $response;
    }

    public function send_location($to, $lat, $lon, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        $data["latitude"]=$lat;
        $data["longitude"]=$lon;
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendLocation", $data);

        return $response;
    }

    public function send_sticker($to, $sticker, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        if(file_exists($sticker))$sticker="@".$sticker;
        $data["sticker"]=$sticker;
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendSticker", $data);

        return $response;
    }

    public function send_video($to, $video, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;
        if(file_exists($video))$video="@".$video;
        $data["video"]=$video;
        if(isset($id_msg))
            $data["reply_to_message_id"]=$id_msg;
        if(isset($reply))
            $data["reply_markup"]=$reply;
        $response = $this->control_api("/sendVideo", $data);
        return $response;
    }

    public function send_photo($to, $photo, $caption, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;

        if(file_exists($photo))$photo="@".$photo;
        $data["photo"]=$photo;
        if(isset($caption)){
            $data["caption"]=$caption;
        }
        if(isset($id_msg)){
            $data["reply_to_message_id"]=$id_msg;
        }
        if(isset($reply))
            $data["reply_markup"]=$reply;

        $response = $this->control_api("/sendPhoto", $data);

        return $response;
    }

    public function send_audio($to, $audio, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;

        if(file_exists($audio))$audio="@".$audio;
        $data["audio"]=$audio;
        if(isset($id_msg)){
            $data["reply_to_message_id"]=$id_msg;
        }
        if(isset($reply))
            $data["reply_markup"]=$reply;

        $response = $this->control_api("/sendAudio", $data);

        return $response;
    }

    public function send_document($to, $document, $id_msg=null, $reply=null){
        $data = array();
        $data["chat_id"]=$to;

        if(file_exists($audio))$document="@".$audio;
        $data["document"]=$document;
        if(isset($id_msg)){
            $data["reply_to_message_id"]=$id_msg;
        }
        if(isset($reply))
            $data["reply_markup"]=$reply;

        $response = $this->control_api("/sendDocument", $data);

        return $response;
    }

    public function forward_message($to, $from, $msg_id){
        $data = array();
        $data["chat_id"]=$to;
        $data["from_chat_id"]=$from;
        $data["message_id"]=$msg_id;
        $response = $this->control_api("/forwardMessage", $data);

        return $response;
    }

    public function set_webhook($url=null){
        $data = array();
        $data["url"]=$url;
        $response = $this->control_api("/setWebhook", $data);

        return $response;
    }

    public function get_user_profile_photos($id_user, $offset=null, $limit=null){
        $data = array();
        $data["user_id"]=$id_user;
        if(isset($offset)){
            $data["offset"]=$offset;
        }
        if(isset($limit)){
            $data["limit"]=$limit;
        }

        $response = $this->control_api("/getUserProfilePhotos", $data);

        return $response;
    }

    public function read_post_message(){
        return json_decode(file_get_contents('php://input'));
    }

}
?>

但是当运行send.php不向频道发送消息时,但当我将chat_id替换为private chat_id或group chat_id时,这段代码做得非常酷,可以向群组或我的联系人发送消息


对不起,请原谅我的英语,抱歉:)

机器人必须是频道的管理员,才能发送消息。没有一个客户端可以帮助您使bot成为频道的管理员。你确定你的机器人是频道的管理员吗


来源:

Bot必须是频道管理员,才能发送消息。没有一个客户端可以帮助您使bot成为频道的管理员。你确定你的机器人是频道的管理员吗


来源:

已解决,我使用HttpRequester插件测试了向此链接发送post请求:

*******************************M/sendMessage

发布的参数为: 聊天室id=@myChannel\u用户名 text=myMessage

我不知道为什么不使用我的旧代码,而是使用这个请求:D


无论如何,谢谢。

解决了,我用HttpRequester插件测试了向此链接发送post请求:

*******************************M/sendMessage

发布的参数为: 聊天室id=@myChannel\u用户名 text=myMessage

我不知道为什么不使用我的旧代码,而是使用这个请求:D


无论如何,谢谢。

curl对于SSL有这个问题。 添加此代码以解决问题:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl对于SSL有这个问题。 添加此代码以解决问题:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
您还可以使用您的频道/组/帐户的数字聊天id发送消息。
获取聊天室id的简单方法是将任何消息从您的频道转发到名为@userinfobot的bot

您还可以使用频道/组/帐户的数字聊天id发送消息。
获取聊天室id的简单方法是将任何消息从您的频道转发到名为@userinfobot的bot

这是正确的,但如果是管理员,bot可以向频道发送消息,或者可以向所有开始与bot对话的人发送消息。来源:这是正确的,但如果是管理员,机器人可以向频道发送消息,或者可以向所有与机器人对话的人发送消息。来源:请检查此项,将有助于提高您的内容质量请检查此项,将有助于提高您的内容质量确保这是频道使用chat_id=@xxxxxyxyx,如果这是用于组,则您必须使用chat_id=-xxyxxyxyx(-前面)。我也有同样的问题并解决了。确保如果这是频道使用chat_id=@xxxyxxyxyx,如果这是用于组,则必须使用chat_id=-xxyxxyyx(-在前面)。我也有同样的问题并解决了。