PHP+Telegram:5 种简单的发送消息的方法
在在线交流领域,Telegram 是一个广受欢迎的消息平台。如果您是开发人员,了解如何轻松地通过代码向 Telegram 发送消息可能会对您有所帮助。本文将向您展示 5 种使用 PHP 轻松实现此操作的方法。
先决条件:
- 已安装 PHP 并启用 curl 扩展
- 已创建公共电报频道
- 使用 BotFather 创建的 Telegram 机器人
- 机器人已添加到频道管理员
电报API
Telegram 提供了多种 API 端点和方法,可以在文档中找到。
今天我们将重点介绍sendMessage端点。它通过机器人{TEXT}
向特定频道发送一些文本{CHANNEL_ID}
{BOT_API_TOKEN}
https://api.telegram.org/bot{BOT_API_TOKEN}/sendMessage?chat_id={CHANNEL_ID}&text={TEXT}
在 PHP 项目中,有很多方法可以调用此端点并向通道发送消息。我们将介绍其中的 5 种。
使用 cURL 的 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 客户端。它允许你轻松地发出 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 频道发送消息的 Symfony 组件是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 应用程序内部
在内部配置通知程序聊天传输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));
}
}
Telegram 机器人 SDK
Telegram Bot SDK让您轻松使用 PHP 开发 Telegram 机器人!支持 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 应用内部
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 发送 Telegram 消息并没有绝对的“正确”方法。您的选择取决于您的需求和所使用的技术。本文介绍了五种使用 PHP 向 Telegram 发送消息的简单方法,实际上,方法远不止五种,具体使用哪种方法取决于您自己。
鏂囩珷鏉ユ簮锛�https://dev.to/p1ngger/phptelegram-5-easy-ways-how-to-send-a-message-5an9