Commit 583799ab authored by MBoretto's avatar MBoretto

Merge branch 'master' into develop

parents b1b32ed2 cdbec9fe
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
......@@ -12,11 +12,11 @@ before_install:
- composer self-update
install:
- travis_retry composer install --no-interaction --prefer-source
- travis_retry composer install --no-interaction
script:
- phpunit
- sh -c "if [ '$TRAVIS_PHP_VERSION' != '7.0' ]; then vendor/bin/phpcs --report=full --extensions=php -p --standard=build/phpcs .; fi"
- vendor/bin/phpunit
- sh -c "if [ '$TRAVIS_PHP_VERSION' != '7.0' ]; then vendor/bin/phpcs --report=full --extensions=php -np --standard=build/phpcs .; fi"
matrix:
allow_failures:
......
......@@ -5,11 +5,11 @@ Before you contribute code to php-telegram-bot, please make sure it conforms to
cd php-telegram-bot
composer update
vendor/bin/phpcs --report=full --extensions=php -p --standard=build/phpcs .
vendor/bin/phpcs --report=full --extensions=php -np --standard=build/phpcs .
Which should give you no output, indicating that there are no coding standard errors. And then:
phpunit
vendor/bin/phpunit
Which should give you no failures or errors. You can ignore any skipped tests as these are for external tools.
......@@ -18,7 +18,7 @@ Pushing
Development is based on the git flow branching model (see http://nvie.com/posts/a-successful-git-branching-model/ )
If you fix a bug please push in hotfix branch.
If you develop a new feature please create a new branch.
If you develop a new feature please create a new branch.
Version
-------
......
The MIT License (MIT)
The [MIT License](http://opensource.org/licenses/mit-license.php)
Copyright (c) 2015 Avtandil Kikabidze aka LONGMAN
Copyright (c) 2015 [Avtandil Kikabidze aka LONGMAN](https://github.com/akalongman)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
# PHP Telegram Bot
[![Join the chat at
https://gitter.im/akalongman/php-telegram-bot](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/akalongman/php-telegram-bot?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Join the chat at https://gitter.im/akalongman/php-telegram-bot](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/akalongman/php-telegram-bot?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/akalongman/php-telegram-bot.svg?branch=master)](https://travis-ci.org/akalongman/php-telegram-bot)
[![Latest Stable
Version](https://img.shields.io/packagist/v/Longman/telegram-bot.svg)](https://packagist.org/packages/longman/telegram-bot)
[![Latest Stable Version](https://img.shields.io/packagist/v/Longman/telegram-bot.svg)](https://packagist.org/packages/longman/telegram-bot)
[![Total Downloads](https://img.shields.io/packagist/dt/Longman/telegram-bot.svg)](https://packagist.org/packages/longman/telegram-bot)
[![Downloads Month](https://img.shields.io/packagist/dm/Longman/telegram-bot.svg)](https://packagist.org/packages/longman/telegram-bot)
[![License](https://img.shields.io/packagist/l/Longman/telegram-bot.svg)](https://packagist.org/packages/longman/telegram-bot)
[![License](https://img.shields.io/packagist/l/Longman/telegram-bot.svg)](https://github.com/akalongman/php-telegram-bot/LICENSE.md)
A Telegram Bot based on the official [Telegram Bot
API](https://core.telegram.org/bots/api)
A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.org/bots/api)
### Introduction
......@@ -27,7 +24,7 @@ The Bot can:
- supports all types and methods according to Telegram API (2015 October 8).
- handle commands in chat with other bots.
It is ready for the channels support.
It is ready for the channels support.
## Instructions
......@@ -102,7 +99,7 @@ Create *composer.json* file:
"name": "yourproject/yourproject",
"type": "project",
"require": {
"php": ">=5.4.0",
"php": ">=5.5.0",
"longman/telegram-bot": "*"
}
}
......@@ -127,7 +124,7 @@ The bot can handle updates with **webhook** or **getUpdate** method:
## Webhook installation
You need server with https and composer support.
You must set a [WebHook](https://core.telegram.org/bots/api#setwebhook).
Create *set.php* (just edit *example-set.php*) and put into it:
Create *set.php* (just copy and edit *examples/set.php*) and put into it:
```php
<?php
//Composer Loader
......@@ -151,7 +148,7 @@ try {
And open your *set.php* via browser.
After, create *hook.php* (or just edit *example-hook.php*) and put:
After, create *hook.php* (or just copy and edit *examples/hook.php*) and put:
```php
<?php
$loader = require __DIR__.'/vendor/autoload.php';
......@@ -180,7 +177,7 @@ $result = $telegram->setWebHook($url, $certificate_path);
## getUpdate installation
You need the database Mysql active.
Create *getUpdateCLI.php* (just edit *example-getUpdateCLI.php*) and put into it:
Create *getUpdateCLI.php* (just copy and edit *examples/getUpdateCLI.php*) and put into it:
```php
#!/usr/bin/env php
<?php
......@@ -199,7 +196,7 @@ try {
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// log telegram errors
echo $e;
}
}
```
give to the file the permission for execution:
......@@ -223,7 +220,7 @@ $data['chat_id'] = $chat_id;
$result = Request::sendPhoto($data,$this->telegram->getUploadPath().'/'.'image.jpg');
```
If you know the file_id of a previously uploaded file, just provide it in the fist param:
If you know the file_id of a previously uploaded file, just provide it in the fist param:
```php
$data['chat_id'] = $chat_id;
......@@ -302,12 +299,12 @@ $COMMANDS_FOLDER = __DIR__.'/Commands/';
$telegram->addCommandsPath($COMMANDS_FOLDER);
```
Inside *CommandsExamples/* there are some sample that show how to use types.
Inside *examples/Commands/* there are some sample that show how to use types.
### Admin Commands
### Admin Commands
Enabling this feature, the admin bot can perform some super user command like:
- Send message to all chats
- List all the chats started with the bot (new!)
- Send message to all chats
- List all the chats started with the bot (new!)
You can specify one or more admin with this option:
......@@ -329,38 +326,47 @@ array('google_api_key'=>'your_google_api_key_here'));
You can overwrite the default Upload and Download directory with:
```php
$telegram->setDownloadPath("yourpath/Download");
$telegram->setUploadPath("yourpath../Upload");
$telegram->setUploadPath("yourpath../Upload");
```
###Unset Webhook
Edit *example-unset.php* with your credential and execute it.
### Logging
Thrown Exception are stored in TelegramException.log file (in the base directory).
Incoming update (json string from webhook and getUpdates) can be logged on a text file, set those options with the methods:
Incoming update (json string from webhook and getUpdates) can be logged on a text file, set those options with the methods:
```php
$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME.'.log');
```
(New!) Set verbosity to 3, to log also curl requests and responses from the bot to Telegram:
(New!) Set verbosity to 3, to log also curl requests and responses from the bot to Telegram:
```php
$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME.'.log');
$telegram->setLogVerbosity(3);
$telegram->setLogVerbosity(3);
```
-----
-----
This code is available on
[Github](https://github.com/akalongman/php-telegram-bot). Pull
requests are welcome.
[Github](https://github.com/akalongman/php-telegram-bot). Pull requests are welcome.
## Troubleshooting
If you like living on the edge, please report any bugs you find on the
[PHP Telegram Bot
issues](https://github.com/akalongman/php-telegram-bot/issues) page.
[PHP Telegram Bot issues](https://github.com/akalongman/php-telegram-bot/issues) page.
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for information.
## License
Please see the [LICENSE](LICENSE.md) included in this repository for a full copy of the MIT license,
which this project is licensed under.
## Credits
Credit list in [CREDITS](CREDITS)
2015-11-24 20:08:51 exception 'Longman\TelegramBot\Exception\TelegramException' with message 'Bot Username not defined!' in /media/D/applications/repositories/php-telegram-bot/src/Telegram.php:168
Stack trace:
#0 /media/D/applications/repositories/php-telegram-bot/tests/Unit/TelegramTest.php(43): Longman\TelegramBot\Telegram->__construct('testapikey', NULL)
#1 [internal function]: Tests\Unit\TelegramTest->newInstanceWithoutParams()
#2 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(908): ReflectionMethod->invokeArgs(Object(Tests\Unit\TelegramTest), Array)
#3 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(771): PHPUnit_Framework_TestCase->runTest()
#4 phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php(617): PHPUnit_Framework_TestCase->runBare()
#5 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(727): PHPUnit_Framework_TestResult->run(Object(Tests\Unit\TelegramTest))
#6 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#7 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#8 phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php(425): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#9 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(154): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#10 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(105): PHPUnit_TextUI_Command->run(Array, true)
#11 /usr/local/bin/phpunit(509): PHPUnit_TextUI_Command::main()
#12 {main}
2015-11-24 20:31:17 exception 'Longman\TelegramBot\Exception\TelegramException' with message 'Bot Username not defined!' in /media/D/applications/repositories/php-telegram-bot/src/Telegram.php:168
Stack trace:
#0 /media/D/applications/repositories/php-telegram-bot/tests/Unit/TelegramTest.php(43): Longman\TelegramBot\Telegram->__construct('testapikey', NULL)
#1 [internal function]: Tests\Unit\TelegramTest->newInstanceWithoutParams()
#2 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(908): ReflectionMethod->invokeArgs(Object(Tests\Unit\TelegramTest), Array)
#3 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(771): PHPUnit_Framework_TestCase->runTest()
#4 phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php(617): PHPUnit_Framework_TestCase->runBare()
#5 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(727): PHPUnit_Framework_TestResult->run(Object(Tests\Unit\TelegramTest))
#6 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#7 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#8 phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php(425): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#9 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(154): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#10 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(105): PHPUnit_TextUI_Command->run(Array, true)
#11 /usr/local/bin/phpunit(509): PHPUnit_TextUI_Command::main()
#12 {main}
......@@ -13,13 +13,18 @@
<!-- Exclude 3rd party libraries and Framework code. -->
<exclude-pattern type="relative">vendor/*</exclude-pattern>
<!-- Include all sniffs in an external standard directory -->
<arg name="colors"/>
<!-- Include some additional sniffs from the Generic standard -->
<rule ref="PSR2">
<exclude name="PEAR.Functions.FunctionCallSignature"/>
<exclude name="PEAR.Functions.FunctionCallSignature.SpaceAfterCloseBracket"/>
</rule>
<rule ref="Generic.Files.LineLength">
<properties>
<property phpcs-only="true" name="lineLimit" value="100"/>
<property phpcbf-only="true" name="lineLimit" value="120"/>
</properties>
</rule>
</ruleset>
......@@ -11,10 +11,10 @@
},
"authors": [
{
"name": "Avtandil Kikabidze aka LONGMAN",
"email": "akalongman@gmail.com",
"homepage": "http://longman.ge",
"role": "Developer"
"name": "Avtandil Kikabidze aka LONGMAN",
"email": "akalongman@gmail.com",
"homepage": "http://longman.me",
"role": "Developer"
}
],
"require": {
......@@ -32,10 +32,9 @@
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "4.1.*",
"phpunit/phpunit": "~4.1",
"phpspec/phpspec": "~2.1",
"squizlabs/php_codesniffer": "2.3.*"
"squizlabs/php_codesniffer": "~2.3"
}
}
This diff is collapsed.
......@@ -4,12 +4,18 @@
#while true; do ./getUpdatesCLI.php; done
//Composer Loader
$loader = require __DIR__.'/vendor/autoload.php';
$dir = realpath(__DIR__.'/..');
$loader = require $dir.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
//$COMMANDS_FOLDER = __DIR__.'/Commands/';
$credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass', 'database'=>'dbname');
$credentials = array(
'host'=>'localhost',
'user'=>'dbuser',
'password'=>'dbpass',
'database'=>'dbname'
);
try {
// create Telegram API object
......
<?php
//Composer Loader
$loader = require __DIR__.'/vendor/autoload.php';
$dir = realpath(__DIR__.'/..');
$loader = require $dir.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
//$COMMANDS_FOLDER = __DIR__.'/Commands/';
//$credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass', 'database'=>'dbname');
/*$credentials = array(
'host'=>'localhost',
'user'=>'dbuser',
'password'=>'dbpass',
'database'=>'dbname'
);*/
try {
// create Telegram API object
......
<?php
//Composer Loader
$loader = require __DIR__.'/vendor/autoload.php';
$dir = realpath(__DIR__.'/..');
$loader = require $dir.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
......
......@@ -57,7 +57,8 @@ abstract class Command
abstract public function execute();
//this methods is executed if $need_mysql is true but DB connection for some reason is not avaiable
// this methods is executed if $need_mysql is true
// but DB connection for some reason is not avaiable
public function executeNoDB()
{
......
......@@ -199,15 +199,7 @@ class Message extends Entity
public function getFullCommand()
{
if (substr($this->text, 0, 1) === '/') {
$no_EOL = strtok($this->text, PHP_EOL);
$no_space = strtok($this->text, ' ');
//try to understand which separator \n or space divide /command from text
if (strlen($no_space) < strlen($no_EOL)) {
return $no_space;
} else {
return $no_EOL;
}
return strtok($this->text, ' ');
} else {
return;
}
......@@ -281,8 +273,7 @@ class Message extends Entity
if ($without_cmd) {
$command = $this->getFullCommand();
if (!empty($command)) {
//$text = substr($text, strlen($command.' '), strlen($text));
$text = substr($text, strlen($command) + 1, strlen($text));
$text = substr($text, strlen($command.' '), strlen($text));
}
}
......
......@@ -17,7 +17,11 @@ class TelegramException extends \Exception
parent::__construct($message, $code);
$path = 'TelegramException.log';
$status = file_put_contents($path, date('Y-m-d H:i:s', time()) .' '. self::__toString() . "\n", FILE_APPEND);
$status = file_put_contents(
$path,
date('Y-m-d H:i:s', time()) .' '. self::__toString() . "\n",
FILE_APPEND
);
}
}
......@@ -100,7 +100,12 @@ class Request
if (isset($data['chat_id'])) {
$data['message_id'] = '1234';
$data['date'] = '1441378360';
$data['from'] = array( 'id' => 123456789 ,'first_name' => 'botname', 'username'=> 'namebot');
$data['from'] = array(
'id' => 123456789,
'first_name' =>
'botname',
'username'=> 'namebot'
);
$data['chat'] = array('id'=> $data['chat_id'] );
$fake_response['result'] = $data;
......@@ -152,7 +157,9 @@ class Request
//Logging getUpdates Update
//Logging curl updates
if ($action == 'getUpdates' & self::$telegram->getLogVerbosity() >=1 | self::$telegram->getLogVerbosity() >=3) {
if ($action == 'getUpdates'
& self::$telegram->getLogVerbosity() >=1
| self::$telegram->getLogVerbosity() >=3) {
self::setInputRaw($result);
self::log($result);
}
......@@ -211,7 +218,7 @@ class Request
curl_close($ch);
# close local file
fclose($fp);
if (filesize($loc_path) > 0) {
return true;
} else {
......
<?php
/*
* This file is part of the TelegramBot package.
*
......@@ -30,7 +29,7 @@ class Telegram
*
* @var string
*/
protected $version = '0.21.1';
protected $version = '0.20.2';
/**
* Telegram API key
......@@ -537,6 +536,7 @@ class Telegram
//$str[0] = strtolower($str[0]);
return $str;
}
/**
* Enable Admin Account
*
......@@ -550,7 +550,7 @@ class Telegram
if ($admin > 0) {
$this->admins_list[] = $admin;
} else {
throw new TelegramException('Only users can be admin not chat!');
throw new TelegramException('Invalid value "'.$admin.'" for admin!');
}
}
......@@ -564,7 +564,6 @@ class Telegram
*
* @return bool
*/
public function isDbEnabled()
{
if ($this->mysql_enabled) {
......
......@@ -86,26 +86,5 @@ class MessageTest extends TestCase
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals('/help@testbot some text', $this->message->getText());
$this->assertEquals('some text', $this->message->getText(true));
// /commmad\n text
$this->message = new Message($this->generateMessage("/help\n some text"), 'testbot');
$this->assertEquals('/help', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals("/help\n some text", $this->message->getText());
$this->assertEquals(' some text', $this->message->getText(true));
// /command@bot\nsome text
$this->message = new Message($this->generateMessage("/help@testbot\nsome text"), 'testbot');
$this->assertEquals('/help@testbot', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals("/help@testbot\nsome text", $this->message->getText());
$this->assertEquals('some text', $this->message->getText(true));
// /command@bot \nsome text
$this->message = new Message($this->generateMessage("/help@testbot \nsome text"), 'testbot');
$this->assertEquals('/help@testbot', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals("/help@testbot \nsome text", $this->message->getText());
$this->assertEquals("\nsome text", $this->message->getText(true));
}
}
......@@ -15,11 +15,11 @@ use \Longman\TelegramBot\Entities\Message;
use \Longman\TelegramBot\Request;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class ServerResponseTest extends TestCase
{
......@@ -27,7 +27,7 @@ class ServerResponseTest extends TestCase
* @var \Longman\TelegramBot\Telegram
*/
private $server;
/**
* setUp
*/
......@@ -39,7 +39,7 @@ class ServerResponseTest extends TestCase
* @test
*/
public function sendMessageOk()
public function sendMessageOk()
{
return '{
"ok":true,
......@@ -55,7 +55,7 @@ class ServerResponseTest extends TestCase
public function testSendMessageOk() {
$result = $this->sendMessageOk();
$result = $this->sendMessageOk();
$this->server = new ServerResponse(json_decode($result, true), 'testbot');
......@@ -82,7 +82,7 @@ class ServerResponseTest extends TestCase
* @test
*/
public function sendMessageFail()
public function sendMessageFail()
{
return '{
"ok":false,
......@@ -110,7 +110,7 @@ class ServerResponseTest extends TestCase
public function setWebHookOk()
{
return '{"ok":true,"result":true,"description":"Webhook was set"}';
}
}
public function testSetWebhookOk() {
......
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