Skip to content

Commit 4d9e1f0

Browse files
committed
Merged sabas in develop
2 parents afe39d7 + 695306e commit 4d9e1f0

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ And run composer update
9090
composer require longman/telegram-bot
9191
```
9292

93-
###### bot token
93+
###### Bot token
9494
You will notice that the Telegram Bot wants a value for `API_KEY`. This token may be obtained via a telegram client for your bot. See [this](https://core.telegram.org/bots#botfather) link if you are unsure of how to so this.
9595

9696

9797
## Usage
98-
You must set [WebHook](https://core.telegram.org/bots/api#setwebhook)
98+
You must set a [WebHook](https://core.telegram.org/bots/api#setwebhook)
9999

100-
Create set.php and put:
100+
Create set.php and put into it:
101101
```php
102102
<?php
103103
$loader = require __DIR__.'/vendor/autoload.php';
@@ -165,9 +165,9 @@ It can execute command triggering a chat event. Here's the list:
165165
- New chat title (**NewchattitleCommand.php**)
166166
- Left chat participant (**LeftchatparticipantCommand.php**)
167167

168-
**GenericCommand.php** let you handle commands that non exists or use commands as variable:
169-
Favourite colour? **/black, /red**
170-
Favourite number? **/1, /134**
168+
**GenericCommand.php** lets you handle commands that don't exist or to use commands as a variable:
169+
Favourite colour? **/black, /red**
170+
Favourite number? **/1, /134**
171171

172172

173173
Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done adding the method:

src/Commands/GenericCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class GenericCommand extends Command
1818
{
1919
protected $name = 'Generic';
20-
protected $description = 'Handle generic commands or is executed by defaul when a command is not found';
20+
protected $description = 'Handle genric commands or is executed by default when a command is not found';
2121
protected $usage = '/';
2222
protected $version = '1.0.0';
2323
protected $enabled = true;
@@ -30,7 +30,10 @@ public function execute()
3030
//you can use $command as param
3131
$command = $message->getCommand();
3232

33-
$data = [];
33+
$chat_id = $message->getChat()->getId();
34+
$text = $message->getText(true);
35+
36+
$data = array();
3437
$data['chat_id'] = $chat_id;
3538
$data['text'] = 'Command: '.$command.' not found.. :(';
3639
$result = Request::sendMessage($data);

src/Commands/SlapCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramBot 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+
*/
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 SlapCommand extends Command
18+
{
19+
protected $name = 'slap';
20+
protected $description = 'Slap someone with their username';
21+
protected $usage = '/slap <@user>';
22+
protected $version = '1.0.0';
23+
protected $enabled = true;
24+
public function execute()
25+
{
26+
$update = $this->getUpdate();
27+
$message = $this->getMessage();
28+
$chat_id = $message->getChat()->getId();
29+
$message_id = $message->getMessageId();
30+
$text = $message->getText(true);
31+
32+
$sender='@'.$message->getFrom()->getUsername();
33+
34+
//username validation
35+
$test=preg_match('/@[\w_]{5,}/', $text);
36+
if ($test===0) {
37+
return false;
38+
}
39+
40+
$data = array();
41+
$data['chat_id'] = $chat_id;
42+
$data['text'] = $sender.' slaps '.$text.' around a bit with a large trout';
43+
$result = Request::sendMessage($data);
44+
return $result;
45+
}
46+
}

0 commit comments

Comments
 (0)