Skip to content

Commit c632d6c

Browse files
committed
v1.0.0
1 parent ae86524 commit c632d6c

12 files changed

+941
-2
lines changed

README.md

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,123 @@
1-
# Basic Telegram Sender PHP
2-
Telegram Group Message Sender
1+
# 🚀 Basic Telegram Sender PHP
2+
3+
A simple PHP script to send messages to a Telegram group using a bot. 🤖
4+
5+
## 📥 Installation
6+
7+
1. **Clone the Repository:**
8+
```sh
9+
git clone https://github.com/jaycee0610/Basic-Telegram-Sender-PHP.git
10+
cd Basic-Telegram-Sender-PHP
11+
```
12+
13+
2. **Install Dependencies:**
14+
```sh
15+
composer require rootscratch/telegram
16+
```
17+
18+
## 🛠️ Setup Guide
19+
20+
### 📌 Step 1: Create a Telegram Bot
21+
- Open Telegram and search for `@BotFather`. 🤖
22+
- Start a chat with `@BotFather` and create a new bot.
23+
- Once created, you will receive a **bot token**. Save this token for later use. 🔑
24+
25+
### 📌 Step 2: Create a Group and Add the Bot
26+
- Create a new Telegram group.
27+
- Add your bot to the group.
28+
- Send any message in the group to initialize it. 🆕
29+
30+
### 📌 Step 3: Get the List of Updates
31+
To retrieve the updates for your bot, use the following API request:
32+
```
33+
https://api.telegram.org/bot<YourBOTToken>/getUpdates
34+
```
35+
**Example:**
36+
```
37+
https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates
38+
```
39+
40+
### 📌 Step 4: Find the Chat ID
41+
Look for the "chat" object in the API response:
42+
```json
43+
{
44+
"update_id": 8393,
45+
"message": {
46+
"message_id": 3,
47+
"from": {
48+
"id": 7474,
49+
"first_name": "AAA"
50+
},
51+
"chat": {
52+
"id": -<group_ID>,
53+
"title": "<Group name>"
54+
},
55+
"date": 25497,
56+
"new_chat_participant": {
57+
"id": 71,
58+
"first_name": "NAME",
59+
"username": "YOUR_BOT_NAME"
60+
}
61+
}
62+
}
63+
```
64+
- The `id` inside the "chat" object is your **Group Chat ID** and always starts with `-`. 🆔
65+
- Use this ID when sending messages to the group. 📩
66+
67+
**Note:**
68+
If you created a new group and the API response only returns:
69+
```json
70+
{"ok": true, "result": []}
71+
```
72+
Try removing and re-adding the bot to the group. 🔄
73+
74+
## 🚀 Usage
75+
76+
1. **Modify `index.php`:**
77+
```php
78+
require_once 'vendor/autoload.php';
79+
80+
use Rootscratch\Telegram\Send;
81+
82+
Send::telegram_token('your-bot-token-here');
83+
Send::telegram_chat_id('-your-chat-id-here');
84+
$message = 'Hello, Telegram!';
85+
86+
try {
87+
$send = Send::SendGroup($message);
88+
echo json_encode($send);
89+
} catch (\Exception $e) {
90+
echo json_encode(['error' => $e->getMessage()]);
91+
}
92+
```
93+
94+
2. **Run the script:**
95+
```sh
96+
php index.php
97+
```
98+
99+
## 📡 API Response Example
100+
```json
101+
{
102+
"ok": true,
103+
"result": {
104+
"message_id": 123,
105+
"chat": {
106+
"id": -1001234567890,
107+
"title": "My Telegram Group"
108+
},
109+
"text": "Hello, Telegram!"
110+
}
111+
}
112+
```
113+
114+
## ⚠️ Troubleshooting
115+
- If you get `{"ok":true,"result":[]}` when retrieving updates, try removing and re-adding the bot to the group. 🔄
116+
- Make sure the bot has permission to send messages in the group. ✅
117+
118+
## 📜 License
119+
This project is licensed under the MIT License. 📝
120+
121+
## 👨‍💻 Author
122+
[John Natividad](https://github.com/jaycee0610)
123+

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "rootscratch/telegram",
3+
"description": "Telegram Group Message Sender PHP",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Rootscratch\\Telegram\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "John Christian Natividad",
14+
"email": "jaycee@rootscratch.com"
15+
}
16+
],
17+
"require": {}
18+
}

example.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use Rootscratch\Telegram\Send;
6+
7+
Send::telegram_token('token');
8+
Send::telegram_chat_id('chat_id');
9+
$message = 'message';
10+
11+
12+
try {
13+
$send = Send::SendGroup($message);
14+
echo json_encode($send);
15+
} catch (\Exception $e) {
16+
echo json_encode(['error' => $e->getMessage()]);
17+
}

src/Send.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Rootscratch\Telegram;
4+
5+
class Send
6+
{
7+
8+
private static $token;
9+
private static $chat_id;
10+
11+
public static function telegram_token($token)
12+
{
13+
self::$token = $token;
14+
}
15+
16+
public static function telegram_chat_id($chat_id)
17+
{
18+
self::$chat_id = $chat_id;
19+
}
20+
21+
public static function SendGroup($message)
22+
{
23+
if (empty(self::$token) || empty(self::$chat_id)) {
24+
throw new \Exception('Token and User ID must be set before sending a message.');
25+
}
26+
27+
$request_params = [
28+
'chat_id' => self::$chat_id,
29+
'text' => $message
30+
];
31+
32+
$request_url = 'https://api.telegram.org/bot' . self::$token . '/sendMessage?' . http_build_query($request_params);
33+
$context = stream_context_create(['http' => ['ignore_errors' => true]]);
34+
$data = file_get_contents($request_url, false, $context);
35+
36+
$responseHeaders = $http_response_header;
37+
$responseCode = null;
38+
if (isset($responseHeaders[0])) {
39+
preg_match('{HTTP\/\S*\s(\d{3})}', $responseHeaders[0], $match);
40+
$responseCode = $match[1];
41+
}
42+
43+
if ($responseCode == 404) {
44+
throw new \Exception('HTTP request failed! HTTP/1.1 404 Not Found');
45+
}
46+
47+
if ($data === false) {
48+
throw new \Exception('Failed to get contents from URL');
49+
}
50+
51+
$decodedData = json_decode($data, true);
52+
if (json_last_error() !== JSON_ERROR_NONE) {
53+
throw new \Exception('JSON decode error: ' . json_last_error_msg());
54+
}
55+
return $decodedData;
56+
}
57+
}

vendor/autoload.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
if (PHP_VERSION_ID < 50600) {
6+
if (!headers_sent()) {
7+
header('HTTP/1.1 500 Internal Server Error');
8+
}
9+
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
10+
if (!ini_get('display_errors')) {
11+
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
12+
fwrite(STDERR, $err);
13+
} elseif (!headers_sent()) {
14+
echo $err;
15+
}
16+
}
17+
trigger_error(
18+
$err,
19+
E_USER_ERROR
20+
);
21+
}
22+
23+
require_once __DIR__ . '/composer/autoload_real.php';
24+
25+
return ComposerAutoloaderInitabead489068f30226339bea3ef0f174a::getLoader();

0 commit comments

Comments
 (0)