在在线沟通的世界中,电报是一个受欢迎的消息平台。如果您是开发人员,那么知道如何轻松从代码中将消息发送到电报可能会很有用。本文将向您展示使用PHP的5种简单方法。
前提:
- php已安装并启用了卷曲扩展
- 创建的公共电报频道
- 使用Botfather创建的电报机器人
- 添加到频道管理员的机器人
电报API
电报提供了各种API端点和方法,可以在文档中找到。
今天,我们将重点关注SendMessage端点。它通过bot {BOT_API_TOKEN}
将一些文本{TEXT}
发送到特定的频道{CHANNEL_ID}
https://api.telegram.org/bot{BOT_API_TOKEN}/sendMessage?chat_id={CHANNEL_ID}&text={TEXT}
有很多方法可以调用此端点并将消息发送到PHP项目中的频道。我们将介绍其中的5个。
带有卷发的HTTP请求
功能风格
<?php
$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from PHP!';
$query = http_build_query([
'chat_id' => $channelId,
'text' => $text,
]);
$url = "https://api.telegram.org/bot{$botApiToken}/sendMessage?{$query}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
));
curl_exec($curl);
curl_close($curl);
OOP风格
安装
composer require curl/curl
用法
<?php
require 'vendor/autoload.php';
$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from PHP!';
$query = http_build_query([
'chat_id' => $channelId,
'text' => $text,
]);
$url = "https://api.telegram.org/bot{$botApiToken}/sendMessage?{$query}";
$curl = new \Curl\Curl();
$curl->get($url);
使用file_get_contents的一行魔术
<?php
$botApiToken = 'your bot api token';
$channelId = 'your channel id';
$text = 'Hello, I am from PHP file_get_contents!';
$query = http_build_query([
'chat_id' => $channelId,
'text' => $text,
]);
$url = "https://api.telegram.org/bot{$botApiToken}/sendMessage?{$query}";
file_get_contents($url);
Symfony HTTP客户
Symfony是最受欢迎的PHP框架之一。它提供了许多不同的组件,其中一个是HTTP Client。它使您可以轻松地提出HTTP请求。
安装
composer require symfony/http-client
用法
<?php
require 'vendor/autoload.php';
use Symfony\Component\HttpClient\HttpClient;
$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from Symfony HTTP Client!';
$client = HttpClient::create([
'base_uri' => 'https://api.telegram.org',
]);
$client->request('GET', "/bot{$botApiToken}/sendMessage", [
'query' => [
'chat_id' => $channelId,
'text' => $text,
],
]);
内部Symfony应用程序
在config/packages/framework.yaml
中准备范围的客户端
framework:
http_client:
scoped_clients:
telegram.client:
base_uri: 'https://api.telegram.org'
并在您的服务中使用它
<?php
namespace App;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class YourService
{
public function __construct(
private readonly HttpClientInterface $telegramClient,
)
{
}
public function sendMessage(string $text): void
{
$botApiToken = 'your bot api token';
$channelId ='your channel id';
$this->telegramClient->request('GET', "/bot{$botApiToken}/sendMessage", [
'query' => [
'chat_id' => $channelId,
'text' => $text,
],
]);
}
}
Symfony Telegram通知
可以将消息发送到电报频道的另一个符号组件是Telegram Notifier。
安装
composer require symfony/telegram-notifier
用法
<?php
require 'vendor/autoload.php';
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransport;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Message\ChatMessage;
$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from Symfony Telegram Notifier!';
$telegramTransport = new TelegramTransport($botApiToken, $channelId);
$chatter = new Chatter($telegramTransport);
$chatMessage = new ChatMessage($text);
$chatter->send($chatMessage);
内部Symfony应用程序
配置Notifier Chatter Transpert
config/packages/framework.yaml
parameters:
env(TELEGRAM_DSN): 'telegram://BOT_API_TOKEN@default?channel=CHANNEL_ID'
framework:
notifier:
chatter_transports:
telegram: '%env(TELEGRAM_DSN)%'
并在您的服务中使用它
<?php
namespace App;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
class YourService
{
public function __construct(
private readonly ChatterInterface $telegramChatter
)
{
}
public function sendMessage(string $text): void
{
$this->telegramChatter->send(new ChatMessage($text));
}
}
TelegramBotâSDK
Telegram Bot SDK允许您轻松地在PHP中开发电报机器人!支持Laravel框架,并带有附加子来增强您的机器人开发体验。
安装
composer require irazasyed/telegram-bot-sdk
用法
<?php
require 'vendor/autoload.php';
use Telegram\Bot\Api;
$botApiToken = 'your bot api token';
$channelId ='your channel id';
$telegram = new Api($botApiToken);
$telegram->sendMessage([
'chat_id' => $channelId,
'text' => $text,
]);
内部LaravelâApp
php artisan vendor:publish --tag="telegram-config"
<?php
namespace App;
use Telegram\Bot\Api;
class YourService
{
public function __construct(private readonly Api $telegram)
{
}
public function sendMessage(string $channelId, string $text): void
{
$this->telegram->sendMessage([
'chat_id' => $channelId,
'text' => $text,
]);
}
}
概括
有很多方法可以实现相同的目标。有了很多可用的选项,没有使用PHP发送电报消息的“正确”方法。您做出的选择取决于您的需求和您正在合作的技术。本文展示了使用PHP将消息发送到电报的五种简单方法,实际上有五个以上,由您决定。