<?php
//输入您的机器人token
$token = "你的机器人token";
//设置连接根地址
$url = "https://api.telegram.org/bot".$token;


//获取信息
$update = json_decode(file_get_contents('php://input'), true);
$chat_id = $update['message']['chat']['id'];
$name = $update['message']['from']['username'];
$message = $update['message'];

//检查get中是否有setWebhookUrl
if (array_key_exists("setWebhookUrl",$_GET)){
    $setWebhookUrl = urlencode($_GET['setWebhookUrl']);
    global $url;
    $url1 = $url."/setWebhook?url=".$setWebhookUrl;
    $str = file_get_contents($url1);
    echo $str;
    return;
}

//检查message中是否有text
if (array_key_exists("text",$message)){
    $text = $message['text'];
    sendTextMsg($text, $chat_id);
}else if (array_key_exists("photo",$message)){
    //0为缩略图
    //1位中等清晰度
    //2位原图
    $photo = $message['photo'][2]['file_id'];
    //caption为文字描述
    if (array_key_exists("caption",$message)){
        sendPhotoMsg($photo, $chat_id, $message['caption']);
    }else{
        sendPhotoMsg($photo, $chat_id);
    }

}


/**
 * 发送文本消息
 * @param String $text 文本消息内容
 * @param String $chat_id 发送的对象
 */
function sendTextMsg($text, $chat_id){
    //发送给用户
    global $url;
    $url1 = "{$url}/sendmessage?text={$text}&chat_id={$chat_id}";
    file_get_contents($url1);
}

/**
 * 发送图片或图文消息
 * @param String $photo 图片file_id
 * @param String $chat_id 发送的对象
 * @param String $caption 图片的说明 默认null
 */
function sendPhotoMsg($photo, $chat_id, $caption=null){
    global $url;
    if($caption){
        $url1 = "{$url}/sendPhoto?photo={$photo}&chat_id={$chat_id}&caption={$caption}";
    }else{
        $url1 = "{$url}/sendPhoto?photo={$photo}&chat_id={$chat_id}";
    }
    file_get_contents($url1);
}
?>

 

发表回复