Skip to content

Commit 73d12c2

Browse files
committed
Implemented echo command
1 parent 51109a8 commit 73d12c2

File tree

12 files changed

+632
-225
lines changed

12 files changed

+632
-225
lines changed

src/Command.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramApi package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramApi;
11+
12+
use Longman\TelegramApi\Entities\Update;
13+
14+
abstract class Command
15+
{
16+
protected $update;
17+
protected $message;
18+
protected $command;
19+
20+
public function __construct(Update $update) {
21+
$this->update = $update;
22+
$this->message = $this->update->getMessage();
23+
24+
}
25+
26+
27+
public abstract function execute();
28+
29+
30+
public function getUpdate() {
31+
return $this->update;
32+
}
33+
34+
public function getMessage() {
35+
return $this->message;
36+
}
37+
38+
public function setCommand($command) {
39+
$this->command = $command;
40+
return $this;
41+
}
42+
43+
44+
45+
}

src/Commands/EchoCommand.php

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

src/Entities/Chat.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramApi package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramApi\Entities;
11+
12+
13+
class Chat
14+
{
15+
16+
protected $id;
17+
protected $title;
18+
protected $first_name;
19+
protected $last_name;
20+
protected $username;
21+
22+
public function __construct(array $data) {
23+
24+
$this->id = isset($data['id']) ? $data['id'] : null;
25+
if (empty($this->id)) {
26+
throw new Exception('id is empty!');
27+
}
28+
29+
}
30+
31+
32+
public function getId() {
33+
34+
return $this->id;
35+
}
36+
37+
38+
public function getTitle() {
39+
40+
return $this->title;
41+
}
42+
43+
public function getFirstName() {
44+
45+
return $this->first_name;
46+
}
47+
48+
public function getLastName() {
49+
50+
return $this->last_name;
51+
}
52+
53+
54+
public function getUsername() {
55+
56+
return $this->username;
57+
}
58+
59+
}

src/Entities/Message.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramApi package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramApi\Entities;
11+
12+
13+
14+
15+
class Message
16+
{
17+
protected $message_id;
18+
19+
protected $from;
20+
21+
protected $date;
22+
23+
protected $chat;
24+
25+
protected $forward_from;
26+
27+
protected $forward_date;
28+
29+
protected $reply_to_message;
30+
31+
protected $text;
32+
33+
protected $audio;
34+
35+
protected $document;
36+
37+
protected $photo;
38+
39+
protected $sticker;
40+
41+
protected $video;
42+
43+
protected $contact;
44+
45+
protected $location;
46+
47+
protected $new_chat_participant;
48+
49+
protected $left_chat_participant;
50+
51+
protected $new_chat_title;
52+
53+
protected $new_chat_photo;
54+
55+
protected $delete_chat_photo;
56+
57+
protected $group_chat_created;
58+
59+
60+
protected $_command;
61+
62+
63+
public function __construct(array $data) {
64+
65+
$this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
66+
if (empty($this->message_id)) {
67+
throw new Exception('message_id is empty!');
68+
}
69+
70+
$this->from = isset($data['from']) ? $data['from'] : null;
71+
if (empty($this->from)) {
72+
throw new Exception('from is empty!');
73+
}
74+
$this->from = new User($this->from);
75+
76+
77+
$this->date = isset($data['date']) ? $data['date'] : null;
78+
if (empty($this->date)) {
79+
throw new Exception('date is empty!');
80+
}
81+
82+
$this->chat = isset($data['chat']) ? $data['chat'] : null;
83+
if (empty($this->chat)) {
84+
throw new Exception('chat is empty!');
85+
}
86+
$this->chat = new Chat($this->chat);
87+
88+
89+
$this->text = isset($data['text']) ? $data['text'] : null;
90+
91+
}
92+
93+
94+
95+
public function getCommand() {
96+
if (!empty($this->_command)) {
97+
return $this->_command;
98+
}
99+
100+
101+
$cmd = strtok($this->text, ' ');
102+
103+
if (substr($cmd, 0, 1) === '/') {
104+
$cmd = substr($cmd, 1);
105+
return $this->_command = $cmd;
106+
}
107+
return false;
108+
}
109+
110+
111+
112+
113+
public function getMessageId() {
114+
115+
return $this->message_id;
116+
}
117+
118+
public function getDate() {
119+
120+
return $this->date;
121+
}
122+
123+
public function getFrom() {
124+
125+
return $this->from;
126+
}
127+
128+
129+
public function getChat() {
130+
131+
return $this->chat;
132+
}
133+
134+
public function getText($without_cmd = false) {
135+
$text = $this->text;
136+
if ($without_cmd) {
137+
$command = $this->getCommand();
138+
$text = substr($text, strlen('/'.$command.' '), strlen($text));
139+
}
140+
141+
return $text;
142+
}
143+
144+
145+
146+
}

src/Entities/Update.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramApi package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramApi\Entities;
11+
12+
13+
14+
15+
class Update
16+
{
17+
18+
protected $update_id;
19+
protected $message;
20+
21+
22+
23+
24+
public function __construct(array $data) {
25+
26+
$update_id = isset($data['update_id']) ? $data['update_id'] : null;
27+
28+
$message = isset($data['message']) ? $data['message'] : null;
29+
30+
if (empty($update_id)) {
31+
throw new Exception('update_id is empty!');
32+
}
33+
34+
$this->update_id = $update_id;
35+
$this->message = new Message($message);
36+
37+
}
38+
39+
public function getUpdateId() {
40+
41+
return $this->update_id;
42+
}
43+
44+
45+
public function getMessage() {
46+
47+
return $this->message;
48+
}
49+
50+
51+
52+
}

0 commit comments

Comments
 (0)