Skip to content

Commit

Permalink
=Add console command for run as bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirocow committed Nov 12, 2015
1 parent 74e212d commit 0aa4c41
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 56 deletions.
7 changes: 6 additions & 1 deletion README.md
@@ -1,5 +1,10 @@
# yii2-telegram-api

``` php

'components' => [
'telegram' => [
'class' => 'mirocow\component\Telegram',
'token' => 'api-telegram-token',
]
],
```
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -18,7 +18,7 @@
},
"autoload": {
"psr-4": {
"mirocow\\telegramm\\api\\": "src"
"mirocow\\telegram\\": "src"
}
}
}
8 changes: 1 addition & 7 deletions src/Client.php
@@ -1,12 +1,6 @@
<?php
/**
* Created by PhpStorm.
* User: mirocow
* Date: 12.11.15
* Time: 0:41
*/

namespace mirocow;
namespace mirocow\telegram;

class Client {

Expand Down
18 changes: 18 additions & 0 deletions src/Module.php
@@ -0,0 +1,18 @@
<?php

namespace mirocow\telegram;

class Module extends \yii\base\Module
{
public $controllerNamespace = 'mirocow\telegram\controllers';

public $token = '';

public $commands = [];

public function init()
{
parent::init();

}
}
32 changes: 0 additions & 32 deletions src/commands/BotController.php

This file was deleted.

19 changes: 19 additions & 0 deletions src/commands/HelpCommand.php
@@ -0,0 +1,19 @@
<?php

namespace mirocow\telegram\commands;

use mirocow\telegram\interfaces\CommandInterface;

class HelpCommand implements CommandInterface {

public function run(\Zelenin\Telegram\Bot\Type\Update $update){

return <<<HELP
Help coomand
HELP;

}

}
15 changes: 0 additions & 15 deletions src/components/Telegram.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/controllers/BotController.php
@@ -0,0 +1,55 @@
<?php

namespace mirocow\telegram\controllers;

use \Zelenin\Telegram\Bot\Api;
use \Zelenin\Telegram\Bot\Daemon\Daemon;
use \Zelenin\Telegram\Bot\Type\Update;
use yii\console\Controller;
use Yii;

class BotController extends Controller
{
public function actionIndex()
{

$client = new Api($this->module->token);

$daemon = new Daemon($client);

$daemon
->onUpdate(function (Update $update) use ($client) {

if(isset($update->message->text) && substr($update->message->text, 0,1) == '/' && $command = str_replace('/','', $update->message->text)) {

if(isset($this->module->commands[$command])) {
$command = $this->module->commands[$command];
} else {
$command = 'mirocow\\telegram\\commands\\' . ucfirst($command) . 'Command';
}

if(class_exists($command)){
$message = (new $command)->run($update);
} else {
$message = Yii::t('app', 'Uncknow command');
}

} else {

$message = '';

}

if($message) {
$response = $client->sendMessage([
'chat_id' => $update->message->chat->id,
'text' => $message
]);
}

});

$daemon->run();

}
}
10 changes: 10 additions & 0 deletions src/interfaces/CommandInterface.php
@@ -0,0 +1,10 @@
<?php

namespace mirocow\telegram\interfaces;

interface CommandInterface
{

function run(\Zelenin\Telegram\Bot\Type\Update $update);

}

0 comments on commit 0aa4c41

Please sign in to comment.