profile_pic

You can fo↑↑ow my blog

WX:_2guagua

2022年6月

利用Telegram的接口,可以实现很方便的消息提醒,不用打开APP,不用科学联网,Telegram的通知就像短信提醒一样。

重点是,免费,无使用数量限制,不用担心短信内容审 查,你想发什么就发什么。

下面是利用php实现的发通知的代码:

<?php
$bot_api_key = 'CHANGE HERE';
function send_get($urlstring){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $urlstring);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 $result = curl_exec($ch);
 curl_close($ch); return $result;
}
$text = @$_GET["text"];
$tgid = @$_GET["tgid"];
if($text){
 $url = "https://api.telegram.org/bot$bot_api_key/sendMessage?chat_id=$tgid&text=$text";
 echo send_get($url);
}else{
 echo "Please Input";
}
?>

传入两个参数,text和tgid。

tgid为需要接收消息的tg帐号ID。

这里使用的telegram接口格式是:

https://api.telegram.org/bot[bot_api_key]/sendMessage?chat_id=[tgid]&text=[text]

接口简单,调用也非常easy,而且还是免费的,没有任何限制,

<?php
function hideStar($str) { //用户名、邮箱、手机账号中间字符串以*隐藏
  if (strpos($str, '@')) {
    $email_array = explode("@", $str);
    $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
    $count = 0;
    $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
    $rs = $prevfix . $str;
  } else {
    $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';
    if (preg_match($pattern, $str)) {
      $rs = preg_replace($pattern, '$1****$2', $str); // substr_replace($name,'****',3,4);
    } else {
      $rs = substr($str, 0, 3) . "***" . substr($str, -1);
    }
  }
  return $rs;
}
?>
<?php
$account = "jb51.net";
$email = "123456789@qq.com";
$phone = "13888888888";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>演示:PHP以星号隐藏用户名手机和邮箱</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <link rel="stylesheet" type="text/css" href="css/common.css" />
    <style type="text/css">
    </style>
  </head>
  <body>
    <div class="head">
      <div class="head_inner clearfix">
        <ul id="nav">
          <li><a href="/">首 页</a></li>
          <li><a href="/templates">网站模板</a></li>
          <li><a href="/js">网页特效</a></li>
          <li><a href="/php">PHP</a></li>
          <li><a href="/site">精选网址</a></li>
        </ul>
        <a class="logo" href=""><img src="images/logo.jpg" alt="素材火logo" /></a>
      </div>
    </div>
    <div class="container">
      <div class="demo">
        <h2 class="title"><a href="#">教程:PHP以星号隐藏用户名手机和邮箱</a></h2>
        <table width="100%" class="table_parameters">
          <tr class="tr_head">
            <td>账号</td>
            <td>邮箱</td>
            <td>手机</td>
          </tr>
          <tr>
            <td><?php echo $account; ?></td>
            <td><?php echo $email; ?></td>
            <td><?php echo $phone; ?></td>
          </tr>
          <tr class="red">
            <td><?php echo hideStar($account); ?></td>
            <td><?php echo hideStar($email); ?></td>
            <td><?php echo hideStar($phone); ?></td>
          </tr>
        </table>
      </div>
    </div>
  </body>
</html>