Skip to content

Commit 9903d7a

Browse files
committed
get update works
1 parent e9f04e4 commit 9903d7a

File tree

10 files changed

+326
-57
lines changed

10 files changed

+326
-57
lines changed

example-getUpdatesCLI.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env php
2+
<?php
3+
#bash script
4+
#while true; do ./getUpdatesCLI.php; done
5+
6+
//Composer Loader
7+
$loader = require __DIR__.'/vendor/autoload.php';
8+
9+
$API_KEY = 'your_bot_api_key';
10+
$BOT_NAME = 'namebot';
11+
//$COMMANDS_FOLDER = __DIR__.'/Commands/';
12+
$credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass', 'database'=>'dbname');
13+
14+
try {
15+
// create Telegram API object
16+
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
17+
18+
//Options
19+
20+
$telegram->enableMySQL($credentials);
21+
//$telegram->enableMySQL($credentials, $BOT_NAME.'_');
22+
//$telegram->addCommandsPath($COMMANDS_FOLDER);
23+
//here you can set some command specified parameters,
24+
//for example, google geocode/timezone api key for date command:
25+
//$telegram->setCommandConfig('date', array('google_api_key'=>'your_google_api_key_here'));
26+
//$telegram->setLogRequests(true);
27+
//$telegram->setLogPath($BOT_NAME.'.log');
28+
29+
30+
// handle telegram getUpdate request
31+
$telegram->handleGetUpdates();
32+
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
33+
// log telegram errors
34+
echo $e->getMessage();
35+
}

example-hook.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
//Composer Loader
3+
$loader = require __DIR__.'/vendor/autoload.php';
4+
5+
$API_KEY = 'your_bot_api_key';
6+
$BOT_NAME = 'namebot';
7+
//$COMMANDS_FOLDER = __DIR__.'/Commands/';
8+
//$credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass', 'database'=>'dbname');
9+
10+
try {
11+
// create Telegram API object
12+
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
13+
14+
//Options
15+
16+
//$telegram->enableMySQL($credentials);
17+
//$telegram->enableMySQL($credentials, $BOT_NAME.'_');
18+
//$telegram->addCommandsPath($COMMANDS_FOLDER);
19+
// here you can set some command specified parameters,
20+
// for example, google geocode/timezone api key for date command:
21+
//$telegram->setCommandConfig('date', array('google_api_key'=>'your_google_api_key_here'));
22+
//$telegram->setLogRequests(true);
23+
//$telegram->setLogPath($BOT_NAME.'.log');
24+
25+
26+
// handle telegram webhook request
27+
$telegram->handle();
28+
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
29+
// log telegram errors
30+
// echo $e->getMessage();
31+
}

src/Admin/SendtoallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SendtoallCommand extends Command
2727
//need Mysql
2828
protected $need_mysql = true;
2929

30-
public function executeFail()
30+
public function executeNoDB()
3131
{
3232
//Database not setted or without connection
3333
//Preparing message

src/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public function preExecute()
5050
) {
5151
return $this->execute();
5252
}
53-
return $this->executeFail();
53+
return $this->executeNoDB();
5454
}
5555

5656
abstract public function execute();
5757

5858
//this methods is executed if $need_mysql is true but DB connection for some reason is not avaiable
59-
public function executeFail()
59+
public function executeNoDB()
6060
{
6161

6262
}

src/DB.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,44 @@ public static function isDbConnected()
8282
}
8383
}
8484

85+
/**
86+
* fetch message from DB
87+
*
88+
* @param fetch Message from the DB
89+
*
90+
* @return bool/ array with data
91+
*/
92+
93+
public static function selectMessages($limit = null)
94+
{
95+
96+
if (!self::isDbConnected()) {
97+
return false;
98+
}
99+
100+
try {
101+
$query = 'SELECT * FROM `'.TB_MESSAGES.'`';
102+
$query .= ' ORDER BY '.TB_MESSAGES.'.`update_id` DESC';
85103

104+
$tokens = [];
105+
if (!is_null($limit)) {
106+
//$query .=' LIMIT :limit ';
107+
//$tokens[':limit'] = $limit;
108+
109+
$query .=' LIMIT '.$limit;
110+
}
111+
//echo $query;
112+
$sth = self::$pdo->prepare($query);
113+
//$sth->execute($tokens);
114+
$sth->execute();
115+
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
116+
117+
} catch (PDOException $e) {
118+
throw new TelegramException($e->getMessage());
119+
}
120+
return $results;
121+
}
122+
86123
/**
87124
* Convert from unix timestamp to timestamp
88125
*
@@ -211,7 +248,7 @@ public static function insertRequest(Update $update)
211248
*
212249
* @return bool
213250
*/
214-
251+
//TODO separe send from query?
215252
public static function sendToActiveChats(
216253
$callback_function,
217254
array $data,

src/Entities/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function __construct(array $data, $bot_name)
104104

105105
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
106106
if (!empty($this->reply_to_message)) {
107-
$this->reply_to_message = new self($this->reply_to_message);
107+
$this->reply_to_message = new Message($this->reply_to_message, $this->bot_name);
108108
}
109109

110110
$this->new_chat_participant = isset($data['new_chat_participant']) ? $data['new_chat_participant'] : null;

src/Entities/ServerResponse.php

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,52 @@ class ServerResponse extends Entity
2222
protected $error_code;
2323
protected $description;
2424

25+
2526
public function __construct(array $data, $bot_name)
2627
{
27-
2828
if (isset($data['ok']) & isset($data['result'])) {
29-
if ($data['ok'] & is_array($data['result'])) {
30-
//Response from sendMessage set
31-
$this->ok = $data['ok'];
32-
$this->result = new Message($data['result'], $bot_name);
33-
$this->error_code = null;
34-
$this->description = null;
29+
if (is_array($data['result'])) {
30+
if ($data['ok'] & !$this->isAssoc($data['result'])) {
31+
//update id
32+
$this->ok = $data['ok'];
33+
//$this->result =[];
34+
foreach ($data['result'] as $update) {
35+
$this->result[] = new Update($update, $bot_name);
36+
}
37+
$this->error_code = null;
38+
$this->description = null;
39+
40+
} elseif ($data['ok'] & $this->isAssoc($data['result'])) {
41+
//Response from sendMessage set
42+
$this->ok = $data['ok'];
43+
$this->result = new Message($data['result'], $bot_name);
44+
$this->error_code = null;
45+
$this->description = null;
46+
}
47+
3548

36-
} elseif ($data['ok'] & $data['result'] == true) {
37-
//Response from setWebhook set
38-
$this->ok = $data['ok'];
39-
$this->result = true;
40-
$this->error_code = null;
4149

42-
if (isset($data['description'])) {
43-
$this->description = $data['description'];
50+
} else {
51+
if ($data['ok'] & $data['result'] == true) {
52+
//Response from setWebhook set
53+
$this->ok = $data['ok'];
54+
$this->result = true;
55+
$this->error_code = null;
56+
57+
if (isset($data['description'])) {
58+
$this->description = $data['description'];
59+
} else {
60+
$this->description = '';
61+
}
4462
} else {
45-
$this->description = '';
63+
$this->ok = false;
64+
$this->result = null;
65+
$this->error_code = $data['error_code'];
66+
$this->description = $data['description'];
4667
}
4768

48-
} else {
49-
$this->ok = false;
50-
$this->result = null;
51-
$this->error_code = $data['error_code'];
52-
$this->description = $data['description'];
5369
}
70+
5471
} else {
5572
//webHook not set
5673
$this->ok = false;
@@ -77,6 +94,11 @@ public function __construct(array $data, $bot_name)
7794
}
7895
}
7996

97+
//must be an array
98+
protected function isAssoc(array $array)
99+
{
100+
return (bool)count(array_filter(array_keys($array), 'is_string'));
101+
}
80102
public function isOk()
81103
{
82104
return $this->ok;

src/Request.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Request
1717
{
1818
private static $telegram;
1919
private static $input;
20+
private static $server_response;
2021

2122
private static $methods = array(
2223
'getMe',
@@ -36,7 +37,11 @@ class Request
3637

3738
public static function initialize(Telegram $telegram)
3839
{
39-
self::$telegram = $telegram;
40+
if (is_object($telegram)) {
41+
self::$telegram = $telegram;
42+
} else {
43+
throw new TelegramException('Telegram pointer is empty!');
44+
}
4045
}
4146

4247
public static function getInput()
@@ -50,6 +55,18 @@ public static function getInput()
5055
return self::$input;
5156
}
5257

58+
public static function getUpdates($data)
59+
{
60+
if ($update = self::$telegram->getCustomUpdate()) {
61+
self::$input = $update;
62+
} else {
63+
self::$input = self::send('getUpdates', $data);
64+
}
65+
self::log(); //TODO
66+
return self::$input;
67+
}
68+
69+
5370
private static function log()
5471
{
5572
if (!self::$telegram->getLogRequests()) {
@@ -126,12 +143,13 @@ public static function send($action, array $data = null)
126143
$response['ok'] = 1;
127144
$response['error_code'] = 1;
128145
$response['description'] = 'Empty server response';
129-
$result =$response;
146+
$result =json_encode($response);
130147
}
131148

132-
133149
//return $result;
134-
return new ServerResponse(json_decode($result, true), self::$telegram->getBotName());
150+
151+
$bot_name = self::$telegram->getBotName();
152+
return new ServerResponse(json_decode($result, true), $bot_name);
135153
}
136154

137155
public static function sendMessage(array $data)

0 commit comments

Comments
 (0)