Skip to content

Commit 15aade2

Browse files
committed
Download file works and fixes
1 parent 5caebd4 commit 15aade2

13 files changed

+188
-44
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ API](https://telegram.org/blog/bot-revolution) allowing integrators of
2222
all sorts to bring automated interactions to the mobile platform. This
2323
Bot aims to provide a platform where one could simply write a plugin
2424
and have interactions in a matter of minutes.
25-
- The Bot supports Reply Markup and handle commands in group chat with
26-
multiple bot.
27-
- The Bot can retrive update with webhook and by getUpdate methods.
25+
The Bot can:
26+
- retrive update with webhook and by getUpdate methods.
27+
- supports all types and methods according to Telegram API (2015 October 8).
28+
- handle commads in chat with other bots.
29+
30+
It is ready for the channels support.
2831

2932

3033
## Instructions
@@ -115,7 +118,7 @@ composer require longman/telegram-bot
115118
### Choose how to retrieve Telegram updates
116119
The bot can handle updates with **webhook** or **getUpdate** method:
117120

118-
| Method | Webhook | getUpdate |
121+
| | Webhook | getUpdate |
119122
| ---- | :----: | :----: |
120123
| Description | Telegram send the update directy to your host | Yuo have to fetch Telegram updates |
121124
| Host with https | Required | Not required |
@@ -148,7 +151,6 @@ try {
148151

149152
And open your *set.php* via browser.
150153

151-
152154
After, create *hook.php* (or just edit *example-hook.php*) and put:
153155
```php
154156
<?php
@@ -168,6 +170,13 @@ try {
168170
// echo $e;
169171
}
170172
```
173+
174+
###Self Signed Certificate
175+
To upload the certificate add the certificate path as param in *set.php*:
176+
```php
177+
$result = $telegram->setWebHook($url, $certificate_path);
178+
```
179+
171180
## getUpdate installation
172181
You need the database Mysql active.
173182

@@ -201,6 +210,18 @@ then run
201210
```
202211
./getUpdateCLI.php
203212
```
213+
### Types
214+
All types implemented according to Telegram API (2015 October 8).
215+
216+
### Methods New!
217+
All methods implemented according to Telegram API (2015 October 8).
218+
TODO example to use methods
219+
220+
```php
221+
$result = Request::sendPhoto($data,'image.jpg');
222+
```
223+
224+
204225
## Utilis
205226
### MySQL storage (Recomended)
206227
If you want insert in database messages/users/chats for further usage
@@ -218,8 +239,6 @@ You can set a custom prefix to all the tables while you are enabling Mysql:
218239
```php
219240
$telegram->enableMySQL($credentials, $BOT_NAME.'_');
220241
```
221-
### Types New!
222-
All types implemented (except InputFile) according to Telegram API (2015 September 18).
223242

224243
### Commands
225244
The bot is able to recognise commands in chat with multiple bot(/command@mybot ).
@@ -268,6 +287,11 @@ example, google geocode/timezone api key for date command:
268287
```php
269288
$telegram->setCommandConfig('date',
270289
array('google_api_key'=>'your_google_api_key_here'));
290+
```
291+
### Upload and Download directory
292+
You can overwrite the default Upload and download directory with:
293+
```php
294+
271295
```
272296
### Send message to all active chats
273297
To do this you have to enable the Mysql connection.
@@ -276,15 +300,12 @@ Here's an example of use:
276300
```php
277301
$results = $telegram->sendToActiveChats(
278302
'sendMessage', //callback function to execute (see Request.php methods)
279-
array('text'=>'Hey! Checkout the new feature!!'), //Param to
280-
evaluate the request
303+
array('text'=>'Hey! Checkout the new feature!!'), //Param to evaluate the request
281304
true, //Send to chats (group chat)
282305
true, //Send to users (single chat)
283306
null, //'yyyy-mm-dd hh:mm:ss' date range from
284307
null //'yyyy-mm-dd hh:mm:ss' date range to
285308
);
286-
287-
print_r($results);
288309
```
289310

290311
### Logging

example-getUpdatesCLI.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
//$telegram->setLogRequests(true);
2727
//$telegram->setLogPath($BOT_NAME.'.log');
2828
//$telegram->setLogVerbosity(3);
29+
//$telegram->setDownloadPath("../Download");
30+
//$telegram->setUploadPath("../Upload");
2931

3032
// handle telegram getUpdate request
3133
$telegram->handleGetUpdates();

example-hook.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//$telegram->setLogPath($BOT_NAME.'.log');
2424
//$telegram->setLogVerbosity(4);
2525

26+
//$telegram->setDownloadPath("../Download");
27+
//$telegram->setUploadPath("../Upload");
2628
// handle telegram webhook request
2729
$telegram->handle();
2830
} catch (Longman\TelegramBot\Exception\TelegramException $e) {

example-set.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
1111
// set webhook
1212
$result = $telegram->setWebHook($link);
13+
//Uncomment to use certificate
14+
//$result = $telegram->setWebHook($link, $path_certificate);
1315
if ($result->isOk()) {
1416
echo $result->getDescription();
1517
}

src/Admin/ChatsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function execute()
6666

6767
$chat = new Chat($result);
6868

69-
if ($chat->isSingleChat()) {
69+
if ($chat->isPrivateChat()) {
7070
//$text .= '- U '.$chat->getFirstName()."\n";
7171

7272
$text .= '- U '.$this->tryMentionChat($chat)."\n";

src/Admin/SendtoallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function execute()
7373

7474
$ServerResponse = $result->getResult();
7575
$chat = $ServerResponse->getChat();
76-
if ($chat->isSingleChat()) {
76+
if ($chat->isPrivateChat()) {
7777
$name = $chat->getFirstName();
7878
$type = 'user';
7979
} else {

src/Commands/StartCommand.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Longman\TelegramBot\Commands;
12+
13+
use Longman\TelegramBot\Request;
14+
use Longman\TelegramBot\Command;
15+
use Longman\TelegramBot\Entities\Update;
16+
17+
class StartCommand extends Command
18+
{
19+
protected $name = 'start';
20+
protected $description = 'Start command';
21+
protected $usage = '/';
22+
protected $version = '1.0.0';
23+
protected $enabled = true;
24+
protected $public = false;
25+
26+
public function execute()
27+
{
28+
$update = $this->getUpdate();
29+
$message = $this->getMessage();
30+
31+
$chat_id = $message->getChat()->getId();
32+
$data = array();
33+
$data['chat_id'] = $chat_id;
34+
35+
$text = "Hi there!\nType /help to see all commands!";
36+
37+
$data['text'] = $text;
38+
$result = Request::sendMessage($data);
39+
return $result;
40+
}
41+
}

src/Commands/WhoamiCommand.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Longman\TelegramBot\Request;
1717
use Longman\TelegramBot\Command;
1818
use Longman\TelegramBot\Entities\Update;
19+
use Longman\TelegramBot\Entities\File;
1920

2021
class WhoamiCommand extends Command
2122
{
@@ -44,7 +45,7 @@ public function execute()
4445
. ' ' . $message->getFrom()->getLastName();
4546
$caption .= "\n" . 'Username: ' . $message->getFrom()->getUsername();
4647

47-
48+
//Fetch user profile photo
4849
$limit = 10;
4950
$offset = null;
5051
$ServerResponse = Request::getUserProfilePhotos([
@@ -54,22 +55,13 @@ public function execute()
5455
]);
5556

5657
//Check if the request isOK
57-
if($ServerResponse->isOk()){
58+
if ($ServerResponse->isOk()) {
5859
$UserProfilePhoto = $ServerResponse->getResult();
5960
$totalcount = $UserProfilePhoto->getTotalCount();
6061
} else {
6162
$totalcount = 0;
6263
}
6364

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-
7365
$data = [];
7466
$data['chat_id'] = $chat_id;
7567
$data['reply_to_message_id'] = $message_id;
@@ -80,13 +72,22 @@ public function execute()
8072
$photo = $photos[0][2];
8173
$file_id = $photo->getFileId();
8274

75+
8376
$data['photo'] = $file_id;
8477
$data['caption'] = $caption;
8578

86-
8779
$result = Request::sendPhoto($data);
88-
} else {
8980

81+
//Download the image pictures
82+
//Download after send message response to speedup response
83+
$file_id = $photo->getFileId();
84+
$ServerResponse = Request::getFile(['file_id' => $file_id]);
85+
if ($ServerResponse->isOk()) {
86+
Request::downloadFile($ServerResponse->getResult());
87+
}
88+
89+
} else {
90+
//No Photo just send text
9091
$data['text'] = $caption;
9192
$result = Request::sendMessage($data);
9293
}

src/DB.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,15 @@ public static function insertRequest(Update $update)
276276
try {
277277
//chats table
278278
$sth2 = self::$pdo->prepare('INSERT INTO `'.TB_CHATS.'`
279-
(`id`, `title`, `created_at` ,`updated_at`)
280-
VALUES (:id, :title, :date, :date)
279+
(`id`, `type`, `title`, `created_at` ,`updated_at`)
280+
VALUES (:id, :type, :title, :date, :date)
281281
ON DUPLICATE KEY UPDATE `title`=:title, `updated_at`=:date');
282282

283283
$chat_title = $chat->getTitle();
284+
$type = $chat->getType();
284285

285286
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
287+
$sth2->bindParam(':type', $type, \PDO::PARAM_INT);
286288
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
287289
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);
288290

src/Entities/Chat.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,34 @@ public function __construct(array $data)
3737
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
3838
$this->username = isset($data['username']) ? $data['username'] : null;
3939
}
40+
4041
public function isGroupChat()
4142
{
42-
if ($this->id < 0) {
43+
if ($this->type == 'group' || $this->id < 0 ) {
4344
return true;
4445
} else {
4546
return false;
4647
}
4748
}
4849

49-
public function isSingleChat()
50+
public function isPrivateChat()
5051
{
51-
if ($this->id > 0) {
52+
if ($this->type == 'private' || $this->id > 0) {
5253
return true;
5354
} else {
5455
return false;
5556
}
5657
}
58+
59+
public function isChannel()
60+
{
61+
if ($this->type == 'channel') {
62+
return true;
63+
} else {
64+
return false;
65+
}
66+
}
67+
5768
public function getId()
5869
{
5970

0 commit comments

Comments
 (0)