Commit a8c27913 authored by Avtandil Kikabidze's avatar Avtandil Kikabidze

Improved code and doc-blocks

parent 87e0d1c4
......@@ -40,7 +40,10 @@ class Botan
public static $command = '';
/**
* Initilize botan
* Initialize Botan
*
* @param $token
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function initializeBotan($token)
{
......@@ -71,7 +74,7 @@ class Botan
* @param string $command
*
* @return bool|string
* @throws TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function track($input, $command = '')
{
......@@ -87,7 +90,7 @@ class Botan
$obj = json_decode($input, true);
if (isset($obj['message'])) {
$data = $obj['message'];
$data = $obj['message'];
$event_name = 'Message';
if (isset($obj['message']['entities']) && is_array($obj['message']['entities'])) {
......@@ -107,13 +110,13 @@ class Botan
}
}
} elseif (isset($obj['inline_query'])) {
$data = $obj['inline_query'];
$data = $obj['inline_query'];
$event_name = 'Inline Query';
} elseif (isset($obj['chosen_inline_result'])) {
$data = $obj['chosen_inline_result'];
$data = $obj['chosen_inline_result'];
$event_name = 'Chosen Inline Result';
} elseif (isset($obj['callback_query'])) {
$data = $obj['callback_query'];
$data = $obj['callback_query'];
$event_name = 'Callback Query';
}
......@@ -121,7 +124,7 @@ class Botan
return false;
}
$uid = $data['from']['id'];
$uid = $data['from']['id'];
$request = str_replace(
['#TOKEN', '#UID', '#NAME'],
[self::$token, $uid, urlencode($event_name)],
......@@ -130,15 +133,15 @@ class Botan
$options = [
'http' => [
'header' => 'Content-Type: application/json',
'method' => 'POST',
'content' => json_encode($data),
'header' => 'Content-Type: application/json',
'method' => 'POST',
'content' => json_encode($data),
'ignore_errors' => true
]
];
$context = stream_context_create($options);
$response = @file_get_contents($request, false, $context);
$context = stream_context_create($options);
$response = @file_get_contents($request, false, $context);
$responseData = json_decode($response, true);
if ($responseData['status'] != 'accepted') {
......@@ -155,7 +158,7 @@ class Botan
* @param $user_id
*
* @return string
* @throws TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function shortenUrl($url, $user_id)
{
......@@ -182,16 +185,17 @@ class Botan
$options = [
'http' => [
'ignore_errors' => true,
'timeout' => 3
'timeout' => 3
]
];
$context = stream_context_create($options);
$context = stream_context_create($options);
$response = @file_get_contents($request, false, $context);
if (!filter_var($response, FILTER_VALIDATE_URL) === false) {
BotanDB::insertShortUrl($user_id, $url, $response);
} else {
// @FIXME: Add telegram log
error_log('Botan.io API replied with error: ' . $response);
return $url;
}
......
......@@ -10,7 +10,9 @@
namespace Longman\TelegramBot;
use Exception;
use Longman\TelegramBot\Exception\TelegramException;
use PDO;
/**
* Class BotanDB
......@@ -33,7 +35,7 @@ class BotanDB extends DB
* @param $user_id
* @param $url
*
* @throws TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*
* @return bool|string
*/
......@@ -48,12 +50,12 @@ class BotanDB extends DB
WHERE `user_id` = :user_id AND `url` = :url
');
$sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth->bindParam(':url', $url, \PDO::PARAM_INT);
$sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$sth->bindParam(':url', $url, PDO::PARAM_INT);
$sth->execute();
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $e) {
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
......@@ -67,7 +69,7 @@ class BotanDB extends DB
* @param $url
* @param $short_url
*
* @throws TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*
* @return bool
*/
......@@ -95,7 +97,7 @@ class BotanDB extends DB
$sth->bindParam(':date', $created_at);
$status = $sth->execute();
} catch (\Exception $e) {
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
......
......@@ -87,9 +87,9 @@ class Conversation
*/
protected function clear()
{
$this->conversation = null;
$this->conversation = null;
$this->protected_notes = null;
$this->notes = null;
$this->notes = null;
return true;
}
......@@ -118,7 +118,7 @@ class Conversation
//Load the conversation notes
$this->protected_notes = json_decode($this->conversation['notes'], true);
$this->notes = $this->protected_notes;
$this->notes = $this->protected_notes;
}
return $this->exists();
......@@ -146,7 +146,8 @@ class Conversation
$this->user_id,
$this->chat_id,
$this->command
)) {
)
) {
return $this->load();
}
}
......@@ -188,7 +189,7 @@ class Conversation
if ($this->exists()) {
$fields = ['status' => $status];
$where = [
'id' => $this->conversation['id'],
'id' => $this->conversation['id'],
'status' => 'active',
'user_id' => $this->user_id,
'chat_id' => $this->chat_id,
......@@ -211,7 +212,7 @@ class Conversation
if ($this->exists()) {
$fields = ['notes' => json_encode($this->notes)];
//I can update a conversation whatever the state is
$where = ['id' => $this->conversation['id']];
$where = ['id' => $this->conversation['id']];
if (ConversationDB::updateConversation($fields, $where)) {
return true;
}
......
......@@ -10,11 +10,10 @@
namespace Longman\TelegramBot;
use Exception;
use Longman\TelegramBot\Exception\TelegramException;
use PDO;
/**
* Class ConversationDB
*/
class ConversationDB extends DB
{
/**
......@@ -35,6 +34,7 @@ class ConversationDB extends DB
* @param bool $limit
*
* @return array|bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function selectConversation($user_id, $chat_id, $limit = null)
{
......@@ -54,14 +54,14 @@ class ConversationDB extends DB
$sth = self::$pdo->prepare($query);
$active = 'active';
$sth->bindParam(':status', $active, \PDO::PARAM_STR);
$sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
$sth->bindParam(':status', $active, PDO::PARAM_STR);
$sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT);
$sth->bindParam(':limit', $limit, PDO::PARAM_INT);
$sth->execute();
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $e) {
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
return $results;
......@@ -75,6 +75,7 @@ class ConversationDB extends DB
* @param string $command
*
* @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function insertConversation($user_id, $chat_id, $command)
{
......@@ -83,7 +84,7 @@ class ConversationDB extends DB
}
try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
(
`status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`
)
......@@ -93,7 +94,7 @@ class ConversationDB extends DB
');
$active = 'active';
//$notes = json_encode('');
$notes = '""';
$notes = '""';
$created_at = self::getTimestamp();
$sth->bindParam(':status', $active);
......@@ -104,7 +105,7 @@ class ConversationDB extends DB
$sth->bindParam(':date', $created_at);
$status = $sth->execute();
} catch (\Exception $e) {
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
return $status;
......@@ -133,6 +134,7 @@ class ConversationDB extends DB
* @todo This function is generic should be moved in DB.php
*
* @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function update($table, array $fields_values, array $where_fields_values)
{
......@@ -143,10 +145,10 @@ class ConversationDB extends DB
$fields_values['updated_at'] = self::getTimestamp();
//Values
$update = '';
$tokens = [];
$update = '';
$tokens = [];
$tokens_counter = 0;
$a = 0;
$a = 0;
foreach ($fields_values as $field => $value) {
if ($a) {
$update .= ', ';
......@@ -158,8 +160,8 @@ class ConversationDB extends DB
}
//Where
$a = 0;
$where = '';
$a = 0;
$where = '';
foreach ($where_fields_values as $field => $value) {
if ($a) {
$where .= ' AND ';
......@@ -168,15 +170,15 @@ class ConversationDB extends DB
$where .= 'WHERE ';
}
++$tokens_counter;
$where .= '`' . $field .'`= :' . $tokens_counter;
$where .= '`' . $field . '`= :' . $tokens_counter;
$tokens[':' . $tokens_counter] = $value;
}
$query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
try {
$sth = self::$pdo->prepare($query);
$sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens);
} catch (\Exception $e) {
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
return $status;
......
This diff is collapsed.
......@@ -21,7 +21,7 @@ class Request
/**
* Telegram object
*
* @var Telegram
* @var \Longman\TelegramBot\Telegram
*/
private static $telegram;
......@@ -86,13 +86,14 @@ class Request
/**
* Initialize
*
* @param Telegram $telegram
* @param \Longman\TelegramBot\Telegram $telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function initialize(Telegram $telegram)
{
if (is_object($telegram)) {
self::$telegram = $telegram;
self::$client = new Client(['base_uri' => self::$api_base_uri]);
self::$client = new Client(['base_uri' => self::$api_base_uri]);
} else {
throw new TelegramException('Telegram pointer is empty!');
}
......@@ -102,6 +103,7 @@ class Request
* Set input from custom input or stdin and return it
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getInput()
{
......@@ -145,13 +147,13 @@ class Request
//some data to let iniatilize the class method SendMessage
if (isset($data['chat_id'])) {
$data['message_id'] = '1234';
$data['date'] = '1441378360';
$data['from'] = [
$data['date'] = '1441378360';
$data['from'] = [
'id' => 123456789,
'first_name' => 'botname',
'username' => 'namebot',
];
$data['chat'] = ['id' => $data['chat_id']];
$data['chat'] = ['id' => $data['chat_id']];
$fake_response['result'] = $data;
}
......@@ -166,6 +168,7 @@ class Request
* @param array|null $data Data to attach to the execution
*
* @return mixed Result of the HTTP Request
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function execute($action, array $data = null)
{
......@@ -224,6 +227,7 @@ class Request
* @param Entities\File $file
*
* @return boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function downloadFile(File $file)
{
......@@ -260,6 +264,7 @@ class Request
* @param string $file
*
* @return resource
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
protected static function encodeFile($file)
{
......@@ -278,7 +283,8 @@ class Request
* @param string $action
* @param array|null $data
*
* @return Entities\ServerResponse
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function send($action, array $data = null)
{
......@@ -322,13 +328,14 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendMessage(array $data)
{
if (empty($data)) {
throw new TelegramException('Data is empty!');
}
$text = $data['text'];
$text = $data['text'];
$string_len_utf8 = mb_strlen($text, 'UTF-8');
if ($string_len_utf8 > 4096) {
$data['text'] = mb_substr($text, 0, 4096);
......@@ -345,6 +352,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function forwardMessage(array $data)
{
......@@ -358,10 +366,11 @@ class Request
/**
* Send photo
*
* @param array $data
* @param array $data
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendPhoto(array $data, $file = null)
{
......@@ -383,6 +392,7 @@ class Request
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendAudio(array $data, $file = null)
{
......@@ -404,6 +414,7 @@ class Request
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendDocument(array $data, $file = null)
{
......@@ -425,6 +436,7 @@ class Request
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendSticker(array $data, $file = null)
{
......@@ -446,6 +458,7 @@ class Request
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendVideo(array $data, $file = null)
{
......@@ -467,6 +480,7 @@ class Request
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendVoice(array $data, $file = null)
{
......@@ -487,6 +501,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendLocation(array $data)
{
......@@ -503,6 +518,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendVenue(array $data)
{
......@@ -519,6 +535,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendContact(array $data)
{
......@@ -535,6 +552,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendChatAction(array $data)
{
......@@ -551,6 +569,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getUserProfilePhotos(array $data)
{
......@@ -602,6 +621,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getFile(array $data)
{
......@@ -618,6 +638,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function kickChatMember(array $data)
{
......@@ -634,6 +655,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function leaveChat(array $data)
{
......@@ -650,6 +672,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function unbanChatMember(array $data)
{
......@@ -668,6 +691,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getChat(array $data)
{
......@@ -686,6 +710,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getChatAdministrators(array $data)
{
......@@ -704,6 +729,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getChatMembersCount(array $data)
{
......@@ -722,6 +748,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getChatMember(array $data)
{
......@@ -738,6 +765,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function answerCallbackQuery(array $data)
{
......@@ -754,6 +782,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function answerInlineQuery(array $data)
{
......@@ -770,6 +799,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function editMessageText(array $data)
{
......@@ -786,6 +816,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function editMessageCaption(array $data)
{
......@@ -802,6 +833,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function editMessageReplyMarkup(array $data)
{
......@@ -837,6 +869,7 @@ class Request
* @param string $date_to
*
* @return array
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function sendToActiveChats(
$callback_function,
......@@ -857,7 +890,7 @@ class Request
$results = [];
foreach ($chats as $row) {
$data['chat_id'] = $row['chat_id'];
$results[] = call_user_func_array($callback_path . '::' . $callback_function, [$data]);
$results[] = call_user_func_array($callback_path . '::' . $callback_function, [$data]);
}
return $results;
......
......@@ -16,7 +16,6 @@ define('BASE_COMMANDS_PATH', BASE_PATH . '/Commands');
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
class Telegram
{
/**
......@@ -152,6 +151,7 @@ class Telegram
*
* @param array $credential
* @param string $table_prefix
* @param string $encoding
*
* @return \Longman\TelegramBot\Telegram
*/
......
......@@ -15,9 +15,6 @@ use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use Longman\TelegramBot\Exception\TelegramLogException;
/**
* Class TelegramLog.
*/
class TelegramLog
{
/**
......@@ -99,6 +96,7 @@ class TelegramLog
* @param string $path
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
*/
public static function initErrorLog($path)
{
......@@ -120,6 +118,7 @@ class TelegramLog
* @param string $path
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
*/
public static function initDebugLog($path)
{
......@@ -162,9 +161,9 @@ class TelegramLog
if (self::$debug_log_temp_stream_handle !== null) {
rewind(self::$debug_log_temp_stream_handle);
self::debug(sprintf(
$message,
stream_get_contents(self::$debug_log_temp_stream_handle)
));
$message,
stream_get_contents(self::$debug_log_temp_stream_handle)
));
fclose(self::$debug_log_temp_stream_handle);
self::$debug_log_temp_stream_handle = null;
}
......@@ -179,6 +178,7 @@ class TelegramLog
* @param string $path
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
*/
public static function initUpdateLog($path)
{
......@@ -189,7 +189,7 @@ class TelegramLog
if (self::$monolog_update === null) {
self::$monolog_update = new Logger('bot_update_log');
// Create a formatter
$output = "%message%\n";
$output = "%message%\n";
$formatter = new LineFormatter($output);
// Update handler
......
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