Commit 46df2f1f authored by MBoretto's avatar MBoretto

Calc Command moved

parent 5fdee26f
...@@ -15,7 +15,7 @@ A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.or ...@@ -15,7 +15,7 @@ A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.or
## introduction ## introduction
This is a pure php Telegram Bot, fully extensible via plugins. Telegram recently announced official support for a [Bot API](https://telegram.org/blog/bot-revolution) allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one could simply write a plugin and have interactions in a matter of minutes. This is a pure php Telegram Bot, fully extensible via plugins. Telegram recently announced official support for a [Bot API](https://telegram.org/blog/bot-revolution) allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one could simply write a plugin and have interactions in a matter of minutes.
The Bot support Reply Markup and handle commands in the group chat. The Bot support Reply Markup and handle commands in group chat.
Instructions Instructions
...@@ -151,7 +151,28 @@ $credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass' ...@@ -151,7 +151,28 @@ $credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass'
$telegram->enableMySQL($credentials); $telegram->enableMySQL($credentials);
``` Utilis
------
Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done adding before the method:
'''php
$COMMANDS_FOLDER = __DIR__.'/Commands/';
$telegram->addCommandsPath($COMMANDS_FOLDER);
'''
You can also log incoming messages on a text file, set this option with the methods:
'''php
$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME.'.log');
'''
This code is available on [Github][0]. Pull requests are welcome. This code is available on [Github][0]. Pull requests are welcome.
......
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
"require": { "require": {
"php": ">=5.4.0", "php": ">=5.4.0",
"ext-pdo": "*", "ext-pdo": "*",
"ext-curl": "*", "ext-curl": "*"
"hoa/math": "~0.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
<?php
/*
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class CalcCommand extends Command
{
protected $name = 'calc';
protected $description = 'Calculate math expression';
protected $usage = '/calc <expression>';
protected $version = '1.0.0';
protected $enabled = true;
protected $public = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
$data = array();
$data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id;
$data['text'] = $this->compute($text);
$result = Request::sendMessage($data);
return $result;
}
protected function compute($expression)
{
// Load the compiler
$compiler = \Hoa\Compiler\Llk::load(new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp'));
// Load the visitor, aka the "evaluator"
$visitor = new \Hoa\Math\Visitor\Arithmetic();
// Parse the expression
$ast = $compiler->parse($expression);
// Evaluate
$result = $visitor->visit($ast);
return $result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment