Skip to content

Commit

Permalink
Ajustes gerais
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcorrea committed May 8, 2018
1 parent 8f66764 commit 8bf6705
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 233 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"autoload": {
"psr-4": {
"Telegram\\": "src/"
"PhpBrasil\\Telegram\\": "src/"
}
}
}
39 changes: 39 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace PhpBrasil\Telegram;

use Telegram\Bot\Api as Http;
use Telegram\Bot\Objects\Message;

/**
* Class Api
* @package PhpBrasil\Telegram
*/
class Api extends Http
{
/**
* Delete messages of chat history.
*
* <code>
* $params = [
* 'chat_id' => '',
* 'message_id' => '',
* ];
* </code>
*
* @link https://core.telegram.org/bots/api#deletemessage
*
* @param array $params
*
* @var int|string $params ['chat_id']
* @var int|string $params ['message_id']
*
* @return Message
*/
public function deleteMessage(array $params)
{
$response = $this->post('deleteMessage', $params);

return new Message($response->getDecodedBody());
}
}
142 changes: 50 additions & 92 deletions src/Bot.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php

namespace Telegram;
namespace PhpBrasil\Telegram;

use Exception;
use Php\File;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\Objects\Message;

/**
* Class Bot
* @package Telegram
* @package PhpBrasil\Telegram
*/
class Bot
{
/**
* @trait
*/
use Request, RequestJson, RequestWebHook, Router;

/**
* @var string
*/
private $token;
use Router, Logger;

/**
* @var string
Expand All @@ -32,63 +29,21 @@ class Bot
private $body;

/**
* Telegram constructor.
* Bot constructor.
* @param string $token
* @throws TelegramSDKException
*/
public function __construct($token)
{
$this->token = $token;
$this->api = "https://api.telegram.org/bot{$token}";
}

/**
* @param $handle
* @return bool|mixed
* @throws Exception
*/
function request($handle)
{
$response = curl_exec($handle);

if ($response === false) {
$errorNumber = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error {$errorNumber}: {$error}\n");
curl_close($handle);
return false;
}

$httpCode = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);

if ($httpCode >= 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
}

if ($httpCode != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
if ($httpCode == 401) {
throw new Exception('Invalid access token provided');
}
return false;
}

$response = json_decode($response, true);
if (isset($response['description'])) {
error_log("Request was successful: {$response['description']}\n");
}
return $response['result'];
$this->api = new Api($token);
}

/**
* @param $filename
* @return Bot
* @param string $filename
* @return $this
* @throws Exception
*/
public function actions($filename)
public function actions(string $filename)
{
if (!File::exists($filename)) {
throw new Exception("File not found `{$filename}`");
Expand All @@ -107,16 +62,12 @@ public function actions($filename)
* @return bool
* @throws Exception
*/
function handle($body)
public function handleText(array $body)
{
$this->body = $body;
$message = $this->body['message'];
$chatId = $message['chat']['id'];
$message = get($this->body, 'message');
if (!isset($message['text'])) {
return $this->apiRequest(
'sendMessage',
['chat_id' => $chatId, 'text' => 'I understand only text messages']
);
return null;
}

$match = $this->match($message);
Expand All @@ -130,65 +81,72 @@ function handle($body)
}

return call_user_func_array(
$callable, [$this, $match, $message]
$callable,
[$this, $match, $message]
);
}

/**
* @param $url
* @throws Exception
* @throws TelegramSDKException
*/
public function remove($url)
public function remove(string $url)
{
$this->apiRequest('setWebhook', ['url' => $url]);
$this->api->setWebhook(['url' => $url]);
}

/**
* @param string $text
* @return bool
* @throws Exception
* @return Message
*/
public function reply($text)
public function reply(string $text)
{
$chatId = $this->body['message']['chat']['id'];
return $this->apiRequest(
'sendMessage', ['chat_id' => $chatId, 'text' => $text]
$chatId = get($this->body, 'message.chat.id');
if (!$chatId) {
return null;
}
return $this->api->sendMessage([
'chat_id' => $chatId,
'text' => $text
]
);
}

/**
* @param string $text
* @return bool
* @throws Exception
* @return Message
*/
public function replyTo($text)
public function replyTo(string $text)
{
$chatId = $this->body['message']['chat']['id'];
$messageId = $this->body['message']['message_id'];
return $this->apiRequest(
'sendMessage',
['chat_id' => $chatId, 'reply_to_message_id' => $messageId, 'text' => $text]
$chatId = get($this->body, 'message.chat.id');
$messageId = get($this->body, 'message.message_id');
return $this->api->sendMessage([
'chat_id' => $chatId,
'reply_to_message_id' => $messageId,
'text' => $text
]
);
}

/**
* @param array $parameters
* @return bool|mixed
* @throws Exception
* @return Message
*/
public function answer($parameters)
public function answer(array $parameters)
{
return $this->apiRequestJson('sendMessage', $parameters);
return $this->api->sendMessage($parameters);
}

/**
* @return bool
* @throws Exception
* @param string|int $messageId
* @return Message
*/
public function delete()
public function delete($messageId = '')
{
$chatId = $this->body['message']['chat']['id'];
$messageId = $this->body['message']['message_id'];
return $this->apiRequest('deleteMessage', ['chat_id' => $chatId, 'message_id' => $messageId]);
$chatId = get($this->body, 'message.chat.id');
if (!$messageId) {
$messageId = get($this->body, 'message.message_id');
}
return $this->api->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
}
}
}
Loading

0 comments on commit 8bf6705

Please sign in to comment.