Skip to content

Commit 5caebd4

Browse files
committed
All methods implemented
1 parent 9179a98 commit 5caebd4

File tree

7 files changed

+280
-31
lines changed

7 files changed

+280
-31
lines changed

src/Commands/WhoamiCommand.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,65 @@ public function execute()
3131
$update = $this->getUpdate();
3232
$message = $this->getMessage();
3333

34+
$user_id = $message->getFrom()->getId();
3435
$chat_id = $message->getChat()->getId();
3536
$message_id = $message->getMessageId();
3637
$text = $message->getText(true);
3738

38-
$data = array();
39+
//send chat action
40+
Request::sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']);
41+
42+
$caption = 'Your Id: ' . $message->getFrom()->getId();
43+
$caption .= "\n" . 'Name: ' . $message->getFrom()->getFirstName()
44+
. ' ' . $message->getFrom()->getLastName();
45+
$caption .= "\n" . 'Username: ' . $message->getFrom()->getUsername();
46+
47+
48+
$limit = 10;
49+
$offset = null;
50+
$ServerResponse = Request::getUserProfilePhotos([
51+
'user_id' => $user_id ,
52+
'limit' => $limit,
53+
'offset' => $offset
54+
]);
55+
56+
//Check if the request isOK
57+
if($ServerResponse->isOk()){
58+
$UserProfilePhoto = $ServerResponse->getResult();
59+
$totalcount = $UserProfilePhoto->getTotalCount();
60+
} else {
61+
$totalcount = 0;
62+
}
63+
64+
65+
66+
//$photos = $UserProfilePhoto->getPhotos();
67+
////I pick the latest photo with the hight definition
68+
//$photo = $photos[0][2];
69+
//$file_id = $photo->getFileId();
70+
//$ServerResponse = Request::getFile(['file_id' => $file_id]);
71+
72+
73+
$data = [];
3974
$data['chat_id'] = $chat_id;
4075
$data['reply_to_message_id'] = $message_id;
41-
$data['text'] = 'Your Id: ' . $message->getFrom()->getId();
42-
$data['text'] .= "\n" . 'Name: ' . $message->getFrom()->getFirstName()
43-
. ' ' . $message->getFrom()->getLastName();
44-
$data['text'] .= "\n" . 'Username: ' . $message->getFrom()->getUsername();
4576

46-
$result = Request::sendMessage($data);
77+
if ( $totalcount > 0){
78+
$photos = $UserProfilePhoto->getPhotos();
79+
//I pick the latest photo with the hight definition
80+
$photo = $photos[0][2];
81+
$file_id = $photo->getFileId();
82+
83+
$data['photo'] = $file_id;
84+
$data['caption'] = $caption;
85+
86+
87+
$result = Request::sendPhoto($data);
88+
} else {
89+
90+
$data['text'] = $caption;
91+
$result = Request::sendMessage($data);
92+
}
4793
return $result;
4894
}
4995
}

src/Entities/Chat.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class Chat extends Entity
1616
{
1717

1818
protected $id;
19+
protected $type;
1920
protected $title;
21+
protected $username;
2022
protected $first_name;
2123
protected $last_name;
22-
protected $username;
2324

2425
public function __construct(array $data)
2526
{
@@ -29,6 +30,8 @@ public function __construct(array $data)
2930
throw new TelegramException('id is empty!');
3031
}
3132

33+
$this->type = isset($data['type']) ? $data['type'] : null;
34+
3235
$this->title = isset($data['title']) ? $data['title'] : null;
3336
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
3437
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
@@ -57,6 +60,12 @@ public function getId()
5760
return $this->id;
5861
}
5962

63+
public function getType()
64+
{
65+
66+
return $this->type;
67+
}
68+
6069
public function getTitle()
6170
{
6271

src/Entities/ServerResponse.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,26 @@ public function __construct(array $data, $bot_name)
2828
if (isset($data['ok']) & isset($data['result'])) {
2929
if (is_array($data['result'])) {
3030
if ($data['ok'] & !$this->isAssoc($data['result'])) {
31-
//update id
32-
$this->ok = $data['ok'];
33-
//$this->result =[];
31+
//get update
3432
foreach ($data['result'] as $update) {
3533
$this->result[] = new Update($update, $bot_name);
3634
}
37-
$this->error_code = null;
38-
$this->description = null;
39-
4035
} elseif ($data['ok'] & $this->isAssoc($data['result'])) {
41-
//Response from sendMessage set
42-
$this->ok = $data['ok'];
43-
$this->result = new Message($data['result'], $bot_name);
44-
$this->error_code = null;
45-
$this->description = null;
36+
if (isset($data['result']['total_count'])) {
37+
//getUserProfilePhotos
38+
$this->result = new UserProfilePhotos($data['result']);
39+
} elseif (isset($data['result']['file_id'])) {
40+
//Response getFile
41+
$this->result = new File($data['result']);
42+
} else {
43+
//Response from sendMessage
44+
$this->result = new Message($data['result'], $bot_name);
45+
}
4646
}
4747

48-
48+
$this->ok = $data['ok'];
49+
$this->error_code = null;
50+
$this->description = null;
4951

5052
} else {
5153
if ($data['ok'] & $data['result'] == true) {
@@ -115,4 +117,7 @@ public function getDescription()
115117
{
116118
return $this->description;
117119
}
120+
public function printError(){
121+
return 'Error N: '.$this->getErrorCode().' Description: '.$this->getDescription();
122+
}
118123
}

src/Entities/UserProfilePhotos.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public function __construct(array $data)
3030
throw new TelegramException('photos is empty!');
3131
}
3232

33-
//TODO check
3433
$photos = [];
35-
foreach ($this->photos as $key => $photos) {
34+
foreach ($this->photos as $key => $photo) {
3635
if (is_array($photo)) {
37-
$photos[] = [new PhotoSize($photo[0])];
36+
foreach ($photo as $photo_size) {
37+
$photos[$key][] = new PhotoSize($photo_size);
38+
}
3839
} else {
3940
throw new TelegramException('photo is not an array!');
4041
}

src/Request.php

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Request
3333
'getUserProfilePhotos',
3434
'getUpdates',
3535
'setWebhook',
36+
'getFile'
3637
);
3738

3839
public static function initialize(Telegram $telegram)
@@ -166,6 +167,11 @@ public static function executeCurl($action, array $data)
166167
return $result;
167168
}
168169

170+
protected static function encodeFile($file)
171+
{
172+
return new \CURLFile($file);
173+
}
174+
169175
public static function send($action, array $data = null)
170176
{
171177
if (!in_array($action, self::$methods)) {
@@ -179,6 +185,9 @@ public static function send($action, array $data = null)
179185

180186
$result = self::executeCurl($action, $data);
181187

188+
echo $result;
189+
print_r(json_decode($result, true));
190+
182191
$bot_name = self::$telegram->getBotName();
183192
return new ServerResponse(json_decode($result, true), $bot_name);
184193
}
@@ -200,11 +209,15 @@ public static function sendMessage(array $data)
200209
return $result;
201210
}
202211

203-
//TODO forwardMessage
204-
205-
protected static function encodeFile($file)
212+
public static function forwardMessage(array $data)
206213
{
207-
return new \CURLFile($file);
214+
215+
if (empty($data)) {
216+
throw new TelegramException('Data is empty!');
217+
}
218+
219+
$result = self::send('forwardMessage', $data);
220+
return $result;
208221
}
209222

210223
public static function sendPhoto(array $data, $file = null)
@@ -310,22 +323,50 @@ public static function sendChatAction(array $data)
310323
return $result;
311324
}
312325

313-
//getUserProfilePhotos
326+
public static function getUserProfilePhotos($data)
327+
{
328+
if (empty($data)) {
329+
throw new TelegramException('Data is empty!');
330+
}
331+
if (!isset($data['user_id'])) {
332+
throw new TelegramException('User id is empty!');
333+
}
334+
335+
$result = self::send('getUserProfilePhotos', $data);
336+
return $result;
337+
}
314338

315339
public static function getUpdates($data)
316340
{
317341
$result = self::send('getUpdates', $data);
318342
return $result;
319343
}
320344

321-
public static function setWebhook($url)
345+
public static function setWebhook($url, $file = null)
322346
{
323-
$result = self::send('setWebhook', array('url' => $url));
347+
if (empty($url)) {
348+
throw new TelegramException('Url is empty!');
349+
}
350+
$data['url'] = $url;
351+
352+
if (!is_null($file)) {
353+
$data['certificate'] = self::encodeFile($file);
354+
}
355+
356+
$result = self::send('setWebhook', $data);
324357
return $result;
325358
}
326359

327-
//getFile
328360

361+
public static function getFile($data)
362+
{
363+
if (empty($data)) {
364+
throw new TelegramException('Data is empty!');
365+
}
366+
367+
$result = self::send('getFile', $data);
368+
return $result;
369+
}
329370
/**
330371
* Send Message in all the active chat
331372
*

src/Telegram.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Telegram
3030
*
3131
* @var string
3232
*/
33-
protected $version = '0.18.1';
33+
protected $version = '0.20.2';
3434

3535
/**
3636
* Telegram API key
@@ -81,6 +81,23 @@ class Telegram
8181
*/
8282
protected $log_path;
8383

84+
/**
85+
* Upload Path
86+
*
87+
* @var string
88+
*/
89+
90+
protected $upload_path;
91+
92+
93+
/**
94+
* Dowload Path
95+
*
96+
* @var string
97+
*/
98+
99+
protected $download_path;
100+
84101
/**
85102
* Log verbosity
86103
*
@@ -117,6 +134,7 @@ class Telegram
117134
protected $message_types = array('text', 'command', 'new_chat_participant',
118135
'left_chat_participant', 'new_chat_title', 'delete_chat_photo', 'group_chat_created'
119136
);
137+
120138
/**
121139
* Admins List
122140
*
@@ -133,11 +151,13 @@ class Telegram
133151

134152
protected $admin_enabled = false;
135153

154+
136155
/**
137156
* Constructor
138157
*
139158
* @param string $api_key
140159
*/
160+
141161
public function __construct($api_key, $bot_name)
142162
{
143163
if (empty($api_key)) {
@@ -348,6 +368,7 @@ public function handleGetUpdates($limit = null, $timeout = null)
348368
print(date('Y-m-d H:i:s', time()).' - Processed '.$a." updates\n");
349369
} else {
350370
print(date('Y-m-d H:i:s', time())." - Fail fetch updates\n");
371+
echo $ServerResponse->printError();
351372
}
352373

353374
//return $results
@@ -566,6 +587,54 @@ public function addCommandsPath($folder)
566587
}
567588

568589

590+
/**
591+
* Set custom upload path
592+
*
593+
* @return object
594+
*/
595+
public function setUploadPath($folder)
596+
{
597+
if (!is_dir($folder)) {
598+
throw new TelegramException('Upload folder not exists!');
599+
}
600+
$this->upload_path = $folder;
601+
return $this;
602+
}
603+
604+
/**
605+
* Get custom upload path
606+
*
607+
* @return string
608+
*/
609+
public function getUploadPath($folder)
610+
{
611+
return $this->upload_path;
612+
}
613+
614+
/**
615+
* Set custom Download path
616+
*
617+
* @return object
618+
*/
619+
public function setDownloadPath($folder)
620+
{
621+
if (!is_dir($folder)) {
622+
throw new TelegramException('Download folder not exists!');
623+
}
624+
$this->upload_path = $folder;
625+
return $this;
626+
}
627+
628+
/**
629+
* Get custom Download path
630+
*
631+
* @return string
632+
*/
633+
public function getDownloadPath($folder)
634+
{
635+
return $this->download_path;
636+
}
637+
569638
/**
570639
* Set command config
571640
*

0 commit comments

Comments
 (0)