Skip to content

Commit 9abfff9

Browse files
author
ParanoiaSystem
committed
Bug Fix + File Upload
1 parent bf110b8 commit 9abfff9

File tree

2 files changed

+82
-41
lines changed

2 files changed

+82
-41
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ $ composer require paranoiasystem/telegrambot-php-library
1818

1919
## Usage
2020

21+
Send Message
22+
2123
``` php
2224
<?php
2325
require_once __DIR__ . '/vendor/autoload.php';
@@ -27,4 +29,21 @@ $ composer require paranoiasystem/telegrambot-php-library
2729
$bot = new TelegramBot('YOUR_BOT_API_TOKEN', 'YOUR_BOT_USERNAME');
2830

2931
$bot->sendMessage('chat_id', 'text');
32+
```
33+
34+
Send Photo
35+
36+
``` php
37+
<?php
38+
require_once __DIR__ . '/vendor/autoload.php';
39+
40+
use Telegram\TelegramBot;
41+
42+
$bot = new TelegramBot('YOUR_BOT_API_TOKEN', 'YOUR_BOT_USERNAME');
43+
44+
$bot->sendPhoto('chat_id', 'path_to_photo');
45+
46+
//or
47+
48+
$bot->sendPhoto('chat_id', array('file_id' => 'file_id_value'));
3049
```

src/TelegramBot.php

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
*
66
* TelegramBot is an unofficial library for use the Telegram APIs for bot.
77
*
8-
* @version 1.0.1
8+
* @version 1.0.2
99
* @author ParanoiaSystem
1010
*/
1111

1212
namespace Telegram;
1313

1414
class TelegramBot
1515
{
16+
1617
function __construct($token, $username){
1718
define('TOKEN', $token);
1819
define('USERNAME', $username);
@@ -28,11 +29,11 @@ function getMe()
2829
function sendMessage($chat_id, $text, $disable_web_page_preview = false, $reply_to_message_id = false, $reply_markup = false){
2930
$arrayPost = array('chat_id' => $chat_id, 'text' => $text);
3031
if($reply_to_message_id)
31-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
32+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
3233
if($disable_web_page_preview)
33-
array_push_assoc($arrayPost, 'disable_web_page_preview', $disable_web_page_preview);
34+
$this->array_push_assoc($arrayPost, 'disable_web_page_preview', $disable_web_page_preview);
3435
if($reply_markup)
35-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
36+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
3637
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendMessage", $arrayPost);
3738
return $jsonResponse;
3839
}
@@ -44,69 +45,69 @@ function forwardMessage($chat_id, $from_chat_id, $message_id){
4445
}
4546

4647
function sendPhoto($chat_id, $photo, $caption = false, $reply_to_message_id = false, $reply_markup = false){
47-
$arrayPost = array('chat_id' => $chat_id, 'photo' => $photo);
48+
$arrayPost = array('chat_id' => $chat_id, 'photo' => $photo);
4849
if($caption)
49-
array_push_assoc($arrayPost, 'caption', $caption);
50+
$this->array_push_assoc($arrayPost, 'caption', $caption);
5051
if($reply_to_message_id)
51-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
52+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
5253
if($reply_markup)
53-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
54+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
5455
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendPhoto", $arrayPost);
5556
return $jsonResponse;
5657
}
5758

5859
function sendAudio($chat_id, $audio, $duration = 0, $reply_to_message_id = false, $reply_markup = false){
5960
$arrayPost = array('chat_id' => $chat_id, 'audio' => $audio);
6061
if($duration > 0)
61-
array_push_assoc($arrayPost, 'duration', $duration);
62+
$this->array_push_assoc($arrayPost, 'duration', $duration);
6263
if($reply_to_message_id)
63-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
64+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
6465
if($reply_markup)
65-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
66+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
6667
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendAudio", $arrayPost);
6768
return $jsonResponse;
6869
}
6970

7071
function sendDocument($chat_id, $document, $reply_to_message_id = false, $reply_markup = false){
7172
$arrayPost = array('chat_id' => $chat_id, 'document' => $document);
7273
if($reply_to_message_id)
73-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
74+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
7475
if($reply_markup)
75-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
76+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
7677
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendDocument", $arrayPost);
7778
return $jsonResponse;
7879
}
7980

8081
function sendSticker($chat_id, $sticker, $reply_to_message_id = false, $reply_markup = false){
8182
$arrayPost = array('chat_id' => $chat_id, 'sticker' => $sticker);
8283
if($reply_to_message_id)
83-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
84+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
8485
if($reply_markup)
85-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
86+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
8687
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendSticker", $arrayPost);
8788
return $jsonResponse;
8889
}
8990

9091
function sendVideo($chat_id, $video, $duration = 0, $caption = false, $reply_to_message_id = false, $reply_markup = false){
9192
$arrayPost = array('chat_id' => $chat_id, 'video' => $video);
9293
if($duration > 0)
93-
array_push_assoc($arrayPost, 'duration', $duration);
94+
$this->array_push_assoc($arrayPost, 'duration', $duration);
9495
if($caption)
95-
array_push_assoc($arrayPost, 'caption', $caption);
96+
$this->array_push_assoc($arrayPost, 'caption', $caption);
9697
if($reply_to_message_id)
97-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
98+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
9899
if($reply_markup)
99-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
100+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
100101
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendVideo", $arrayPost);
101102
return $jsonResponse;
102103
}
103104

104105
function sendLocation($chat_id, $latitude, $longitude, $reply_to_message_id = false, $reply_markup = false){
105106
$arrayPost = array('chat_id' => $chat_id, 'latitude' => $latitude, 'longitude' => $longitude);
106107
if($reply_to_message_id)
107-
array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
108+
$this->array_push_assoc($arrayPost, 'reply_to_message_id', $reply_to_message_id);
108109
if($reply_markup)
109-
array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
110+
$this->array_push_assoc($arrayPost, 'reply_markup', $reply_markup);
110111
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/sendLocation", $arrayPost);
111112
return $jsonResponse;
112113
}
@@ -120,9 +121,9 @@ function sendChatAction($chat_id, $action){
120121
function getUserProfilePhotos($user_id, $offset = false, $limit = false){
121122
$arrayPost = array('user_id' => $user_id);
122123
if($offset)
123-
array_push_assoc($arrayPost, 'offset', $offset);
124+
$this->array_push_assoc($arrayPost, 'offset', $offset);
124125
if($limit)
125-
array_push_assoc($arrayPost, 'limit', $limit);
126+
$this->array_push_assoc($arrayPost, 'limit', $limit);
126127
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/getUserProfilePhotos", $arrayPost);
127128
return $jsonResponse;
128129
}
@@ -131,19 +132,19 @@ function getUpdates($offset = false, $limit = false, $timeout = false)
131132
{
132133
$arrayPost = array();
133134
if ($offset)
134-
array_push_assoc($arrayPost, 'offset', $offset);
135+
$this->array_push_assoc($arrayPost, 'offset', $offset);
135136
if ($limit)
136-
array_push_assoc($arrayPost, 'limit', $limit);
137+
$this->array_push_assoc($arrayPost, 'limit', $limit);
137138
if ($timeout)
138-
array_push_assoc($arrayPost, 'timeout', $timeout);
139+
$this->array_push_assoc($arrayPost, 'timeout', $timeout);
139140
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/getUpdates", $arrayPost);
140141
return $jsonResponse;
141142
}
142143

143144
function setWebhook($url = false){
144145
$arrayPost = array();
145146
if ($url)
146-
array_push_assoc($arrayPost, 'url', $url);
147+
$this->array_push_assoc($arrayPost, 'url', $url);
147148
$jsonResponse = $this->curl_call("https://api.telegram.org/bot" . TOKEN . "/setWebhook", $arrayPost);
148149
return $jsonResponse;
149150
}
@@ -157,22 +158,43 @@ function test($json)
157158
return false;
158159
}
159160

160-
function array_push_assoc(&$array, $key, $value){
161+
private function array_push_assoc(&$array, $key, $value){
161162
$array[$key] = $value;
162163
}
163164

164-
function curl_call($url, $post=array()){
165-
$ch = curl_init($url);
166-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
167-
curl_setopt($ch, CURLOPT_USERAGENT, "ParanoiaBot 1.0");
168-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
169-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
170-
if (count($post)) {
171-
curl_setopt($ch, CURLOPT_POST, count($post));
172-
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
173-
}
174-
$r = curl_exec($ch);
175-
curl_close($ch);
176-
return $r;
165+
private function curl_call($url, $post=array(), $headers=array()){
166+
$attachments = ['photo', 'sticker', 'audio', 'document', 'video'];
167+
$ch = curl_init($url);
168+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
169+
curl_setopt($ch, CURLOPT_USERAGENT, "ParanoiaBot 1.0");
170+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
171+
if (count($post)) {
172+
curl_setopt($ch, CURLOPT_POST, true);
173+
174+
foreach($attachments as $attachment){
175+
if(isset($post[$attachment])){
176+
$post[$attachment] = $this->curlFile($post[$attachment]);
177+
break;
178+
}
179+
}
180+
181+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
182+
}
183+
$r = curl_exec($ch);
184+
curl_close($ch);
185+
return $r;
177186
}
187+
188+
private function curlFile($path){
189+
if (is_array($path))
190+
return $path['file_id'];
191+
192+
$realPath = realpath($path);
193+
194+
if (class_exists('CURLFile'))
195+
return new \CURLFile($realPath);
196+
197+
return '@' . $realPath;
198+
}
199+
178200
}

0 commit comments

Comments
 (0)