Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
818c3c84
Commit
818c3c84
authored
Sep 08, 2016
by
Armando Lüscher
Committed by
GitHub
Sep 08, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #288 from noplanman/scrutinizer_fixes
Scrutinizer fixes
parents
005f781e
b4713f3b
Changes
46
Show whitespace changes
Inline
Side-by-side
Showing
46 changed files
with
1319 additions
and
1203 deletions
+1319
-1203
.scrutinizer.yml
.scrutinizer.yml
+4
-1
ForcereplyCommand.php
examples/Commands/ForcereplyCommand.php
+6
-5
ImageCommand.php
examples/Commands/ImageCommand.php
+20
-12
InlinekeyboardCommand.php
examples/Commands/InlinekeyboardCommand.php
+6
-6
KeyboardCommand.php
examples/Commands/KeyboardCommand.php
+41
-44
MarkdownCommand.php
examples/Commands/MarkdownCommand.php
+9
-8
Botan.php
src/Botan.php
+12
-11
BotanDB.php
src/BotanDB.php
+9
-15
ChatsCommand.php
src/Commands/AdminCommands/ChatsCommand.php
+37
-34
SendtoallCommand.php
src/Commands/AdminCommands/SendtoallCommand.php
+18
-12
SendtochannelCommand.php
src/Commands/AdminCommands/SendtochannelCommand.php
+144
-139
WhoisCommand.php
src/Commands/AdminCommands/WhoisCommand.php
+56
-43
Command.php
src/Commands/Command.php
+14
-7
SystemCommand.php
src/Commands/SystemCommand.php
+1
-1
CallbackqueryCommand.php
src/Commands/SystemCommands/CallbackqueryCommand.php
+7
-10
ChannelchatcreatedCommand.php
src/Commands/SystemCommands/ChannelchatcreatedCommand.php
+12
-4
ChoseninlineresultCommand.php
src/Commands/SystemCommands/ChoseninlineresultCommand.php
+11
-3
DeletechatphotoCommand.php
src/Commands/SystemCommands/DeletechatphotoCommand.php
+11
-3
EditedmessageCommand.php
src/Commands/SystemCommands/EditedmessageCommand.php
+13
-5
GenericCommand.php
src/Commands/SystemCommands/GenericCommand.php
+5
-3
GenericmessageCommand.php
src/Commands/SystemCommands/GenericmessageCommand.php
+3
-2
GroupchatcreatedCommand.php
src/Commands/SystemCommands/GroupchatcreatedCommand.php
+11
-3
InlinequeryCommand.php
src/Commands/SystemCommands/InlinequeryCommand.php
+5
-4
LeftchatmemberCommand.php
src/Commands/SystemCommands/LeftchatmemberCommand.php
+11
-3
MigratefromchatidCommand.php
src/Commands/SystemCommands/MigratefromchatidCommand.php
+11
-3
MigratetochatidCommand.php
src/Commands/SystemCommands/MigratetochatidCommand.php
+11
-3
NewchatmemberCommand.php
src/Commands/SystemCommands/NewchatmemberCommand.php
+6
-4
NewchatphotoCommand.php
src/Commands/SystemCommands/NewchatphotoCommand.php
+11
-3
NewchattitleCommand.php
src/Commands/SystemCommands/NewchattitleCommand.php
+11
-3
StartCommand.php
src/Commands/SystemCommands/StartCommand.php
+3
-2
SupergroupchatcreatedCommand.php
src/Commands/SystemCommands/SupergroupchatcreatedCommand.php
+11
-3
CancelCommand.php
src/Commands/UserCommands/CancelCommand.php
+14
-11
DateCommand.php
src/Commands/UserCommands/DateCommand.php
+33
-28
EchoCommand.php
src/Commands/UserCommands/EchoCommand.php
+2
-1
HelpCommand.php
src/Commands/UserCommands/HelpCommand.php
+32
-13
SlapCommand.php
src/Commands/UserCommands/SlapCommand.php
+4
-4
SurveyCommand.php
src/Commands/UserCommands/SurveyCommand.php
+89
-67
WeatherCommand.php
src/Commands/UserCommands/WeatherCommand.php
+20
-14
WhoamiCommand.php
src/Commands/UserCommands/WhoamiCommand.php
+61
-54
DB.php
src/DB.php
+287
-332
KeyboardButton.php
src/Entities/KeyboardButton.php
+1
-1
ReplyKeyboardMarkup.php
src/Entities/ReplyKeyboardMarkup.php
+18
-16
ServerResponse.php
src/Entities/ServerResponse.php
+12
-30
Request.php
src/Request.php
+143
-196
Telegram.php
src/Telegram.php
+56
-29
TelegramLog.php
src/TelegramLog.php
+17
-8
No files found.
.scrutinizer.yml
View file @
818c3c84
tools
:
external_code_coverage
:
timeout
:
600
filter
:
paths
:
[
"
src/*"
]
examples/Commands/ForcereplyCommand.php
View file @
818c3c84
...
...
@@ -25,7 +25,7 @@ class ForceReplyCommand extends UserCommand
protected
$name
=
'forcereply'
;
protected
$description
=
'Force reply with reply markup'
;
protected
$usage
=
'/forcereply'
;
protected
$version
=
'0.0.
5
'
;
protected
$version
=
'0.0.
6
'
;
/**#@-*/
/**
...
...
@@ -36,10 +36,11 @@ class ForceReplyCommand extends UserCommand
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$data
[
'text'
]
=
'Write something:'
;
$data
[
'reply_markup'
]
=
new
ForceReply
([
'selective'
=>
false
]);
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
'Write something:'
,
'reply_markup'
=>
new
ForceReply
([
'selective'
=>
false
]),
];
return
Request
::
sendMessage
(
$data
);
}
...
...
examples/Commands/ImageCommand.php
View file @
818c3c84
...
...
@@ -25,7 +25,7 @@ class ImageCommand extends UserCommand
protected
$name
=
'image'
;
protected
$description
=
'Send Image'
;
protected
$usage
=
'/image'
;
protected
$version
=
'1.0.
0
'
;
protected
$version
=
'1.0.
1
'
;
/**#@-*/
/**
...
...
@@ -37,18 +37,26 @@ class ImageCommand extends UserCommand
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
$message
->
getText
(
true
);
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$data
[
'caption'
]
=
$text
;
$data
=
[
'chat_id'
=>
$chat_id
,
'caption'
=>
$text
,
];
//
return Request::sendPhoto($data, $this->telegram->getUploadPath().'/'.'image.jpg');
//
Return a random picture from the telegram->getUploadPath().
return
Request
::
sendPhoto
(
$data
,
$this
->
ShowRandomImage
(
$this
->
telegram
->
getUploadPath
()));
}
//return random picture from the telegram->getUploadPath();
private
function
ShowRandomImage
(
$dir
)
{
/**
* Return the path to a random image in the passed directory.
*
* @param string $dir
*
* @return string
*/
private
function
ShowRandomImage
(
$dir
)
{
$image_list
=
scandir
(
$dir
);
return
$dir
.
"/"
.
$image_list
[
mt_rand
(
2
,
count
(
$image_list
)
-
1
)];
}
return
$dir
.
'/'
.
$image_list
[
mt_rand
(
2
,
count
(
$image_list
)
-
1
)];
}
}
examples/Commands/InlinekeyboardCommand.php
View file @
818c3c84
...
...
@@ -26,7 +26,7 @@ class InlinekeyboardCommand extends UserCommand
protected
$name
=
'Inlinekeyboard'
;
protected
$description
=
'Show inline keyboard'
;
protected
$usage
=
'/inlinekeyboard'
;
protected
$version
=
'0.0.
1
'
;
protected
$version
=
'0.0.
2
'
;
/**#@-*/
/**
...
...
examples/Commands/KeyboardCommand.php
View file @
818c3c84
...
...
@@ -23,9 +23,9 @@ class KeyboardCommand extends UserCommand
* {@inheritdoc}
*/
protected
$name
=
'keyboard'
;
protected
$description
=
'Show a custom keybord with reply markup'
;
protected
$description
=
'Show a custom keybo
a
rd with reply markup'
;
protected
$usage
=
'/keyboard'
;
protected
$version
=
'0.
0.6
'
;
protected
$version
=
'0.
1.0
'
;
/**#@-*/
/**
...
...
@@ -35,70 +35,67 @@ class KeyboardCommand extends UserCommand
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
$message
->
getText
(
true
);
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$data
[
'text'
]
=
'Press a Button:'
;
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
'Press a Button:'
,
];
//Keyboard examples
$keyboards
=
[];
//0
$keyboard
[]
=
[
'7'
,
'8'
,
'9'
];
$keyboard
[]
=
[
'4'
,
'5'
,
'6
'
];
$keyboard
[]
=
[
'1'
,
'2'
,
'3
'
];
$keyboard
[]
=
[
' '
,
'0'
,
'
'
];
//
Example
0
$keyboard
=
[
];
$keyboard
[]
=
[
'7'
,
'8'
,
'9
'
];
$keyboard
[]
=
[
'4'
,
'5'
,
'6
'
];
$keyboard
[]
=
[
'1'
,
'2'
,
'3
'
];
$keyboard
[]
=
[
' '
,
'0'
,
' '
];
$keyboards
[]
=
$keyboard
;
unset
(
$keyboard
);
//1
$keyboard
[]
=
[
'7'
,
'8'
,
'9'
,
'+'
];
$keyboard
[]
=
[
'4'
,
'5'
,
'6'
,
'-'
];
$keyboard
[]
=
[
'1'
,
'2'
,
'3'
,
'*'
];
$keyboard
[]
=
[
' '
,
'0'
,
' '
,
'/'
];
//Example 1
$keyboard
=
[];
$keyboard
[]
=
[
'7'
,
'8'
,
'9'
,
'+'
];
$keyboard
[]
=
[
'4'
,
'5'
,
'6'
,
'-'
];
$keyboard
[]
=
[
'1'
,
'2'
,
'3'
,
'*'
];
$keyboard
[]
=
[
' '
,
'0'
,
' '
,
'/'
];
$keyboards
[]
=
$keyboard
;
unset
(
$keyboard
);
//2
//Example 2
$keyboard
=
[];
$keyboard
[]
=
[
'A'
];
$keyboard
[]
=
[
'B'
];
$keyboard
[]
=
[
'C'
];
$keyboards
[]
=
$keyboard
;
unset
(
$keyboard
);
//3
//Example 3
$keyboard
=
[];
$keyboard
[]
=
[
'A'
];
$keyboard
[]
=
[
'B'
];
$keyboard
[]
=
[
'C'
,
'D'
];
$keyboard
[]
=
[
'C'
,
'D'
];
$keyboards
[]
=
$keyboard
;
unset
(
$keyboard
);
//4 (bots 2.0)
//Example 4 (bots version 2.0)
$keyboard
=
[];
$keyboard
[]
=
[
[
'text'
=>
'request_
contact'
,
'request_contact'
=>
true
'text'
=>
'Send my
contact'
,
'request_contact'
=>
true
,
],
[
'text'
=>
'request_
location'
,
'request_location'
=>
true
]
'text'
=>
'Send my
location'
,
'request_location'
=>
true
,
]
,
];
$keyboards
[]
=
$keyboard
;
unset
(
$keyboard
);
//Return a random keyboard.
$keyboard
=
$keyboards
[
mt_rand
(
0
,
count
(
$keyboards
)
-
1
)];
$data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
$keyboards
[
1
]
,
'keyboard'
=>
$keyboard
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
false
,
'selective'
=>
false
'selective'
=>
false
,
]
);
...
...
examples/Commands/MarkdownCommand.php
View file @
818c3c84
...
...
@@ -25,7 +25,7 @@ class MarkdownCommand extends UserCommand
protected
$name
=
'markdown'
;
protected
$description
=
'Print Markdown tesxt'
;
protected
$usage
=
'/markdown'
;
protected
$version
=
'1.0.
0
'
;
protected
$version
=
'1.0.
1
'
;
/**#@-*/
/**
...
...
@@ -35,17 +35,18 @@ class MarkdownCommand extends UserCommand
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
$message
->
getText
(
true
);
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$data
[
'parse_mode'
]
=
'MARKDOWN'
;
$data
[
'text'
]
=
"*bold* _italic_ `inline fixed width code` ```preformatted code block
$data
=
[
'chat_id'
=>
$chat_id
,
'parse_mode'
=>
'MARKDOWN'
,
'text'
=>
'*bold* _italic_ `inline fixed width code`
```
preformatted code block
code block
```
[Best Telegram bot api!!](https://github.com/akalongman/php-telegram-bot)
"
;
'
,
]
;
return
Request
::
sendMessage
(
$data
);
}
...
...
src/Botan.php
View file @
818c3c84
...
...
@@ -78,7 +78,7 @@ class Botan
*/
public
static
function
track
(
$input
,
$command
=
''
)
{
if
(
empty
(
self
::
$token
)
||
$command
!=
self
::
$command
)
{
if
(
empty
(
self
::
$token
)
||
$command
!=
=
self
::
$command
)
{
return
false
;
}
...
...
@@ -89,16 +89,17 @@ class Botan
self
::
$command
=
''
;
$obj
=
json_decode
(
$input
,
true
);
$data
=
[];
if
(
isset
(
$obj
[
'message'
]))
{
$data
=
$obj
[
'message'
];
$event_name
=
'Message'
;
if
(
isset
(
$obj
[
'message'
][
'entities'
])
&&
is_array
(
$obj
[
'message'
][
'entities'
]))
{
foreach
(
$obj
[
'message'
][
'entities'
]
as
$entity
)
{
if
(
$entity
[
'type'
]
==
'bot_command'
&&
$entity
[
'offset'
]
==
0
)
{
if
(
strtolower
(
$command
)
==
'generic'
)
{
if
(
$entity
[
'type'
]
==
=
'bot_command'
&&
$entity
[
'offset'
]
=
==
0
)
{
if
(
strtolower
(
$command
)
==
=
'generic'
)
{
$command
=
'Generic'
;
}
elseif
(
strtolower
(
$command
)
==
'genericmessage'
)
{
}
elseif
(
strtolower
(
$command
)
==
=
'genericmessage'
)
{
$command
=
'Generic Message'
;
}
else
{
$command
=
'/'
.
strtolower
(
$command
);
...
...
@@ -136,15 +137,15 @@ class Botan
'header'
=>
'Content-Type: application/json'
,
'method'
=>
'POST'
,
'content'
=>
json_encode
(
$data
),
'ignore_errors'
=>
true
]
'ignore_errors'
=>
true
,
]
,
];
$context
=
stream_context_create
(
$options
);
$response
=
@
file_get_contents
(
$request
,
false
,
$context
);
$responseData
=
json_decode
(
$response
,
true
);
if
(
$responseData
[
'status'
]
!=
'accepted'
)
{
if
(
$responseData
[
'status'
]
!=
=
'accepted'
)
{
error_log
(
'Botan.io API replied with error: '
.
$response
);
}
...
...
@@ -171,7 +172,6 @@ class Botan
}
$cached
=
BotanDB
::
selectShortUrl
(
$user_id
,
$url
);
if
(
!
empty
(
$cached
[
0
][
'short_url'
]))
{
return
$cached
[
0
][
'short_url'
];
}
...
...
@@ -185,8 +185,8 @@ class Botan
$options
=
[
'http'
=>
[
'ignore_errors'
=>
true
,
'timeout'
=>
3
]
'timeout'
=>
3
,
]
,
];
$context
=
stream_context_create
(
$options
);
...
...
@@ -197,6 +197,7 @@ class Botan
}
else
{
// @TODO: Add telegram log
error_log
(
'Botan.io API replied with error: '
.
$response
);
return
$url
;
}
...
...
src/BotanDB.php
View file @
818c3c84
...
...
@@ -35,9 +35,8 @@ class BotanDB extends DB
* @param $user_id
* @param $url
*
* @return array|bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*
* @return bool|string
*/
public
static
function
selectShortUrl
(
$user_id
,
$url
)
{
...
...
@@ -54,12 +53,10 @@ class BotanDB extends DB
$sth
->
bindParam
(
':url'
,
$url
,
PDO
::
PARAM_INT
);
$sth
->
execute
();
$results
=
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
return
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
}
catch
(
Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$results
;
}
/**
...
...
@@ -69,9 +66,8 @@ class BotanDB extends DB
* @param $url
* @param $short_url
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*
* @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
insertShortUrl
(
$user_id
,
$url
,
$short_url
)
{
...
...
@@ -96,11 +92,9 @@ class BotanDB extends DB
$sth
->
bindParam
(
':short_url'
,
$short_url
);
$sth
->
bindParam
(
':date'
,
$created_at
);
$status
=
$sth
->
execute
();
return
$sth
->
execute
();
}
catch
(
Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$status
;
}
}
src/Commands/AdminCommands/ChatsCommand.php
View file @
818c3c84
...
...
@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand
/**
* @var string
*/
protected
$version
=
'1.
0.2
'
;
protected
$version
=
'1.
1.0
'
;
/**
* @var bool
...
...
@@ -46,6 +46,7 @@ class ChatsCommand extends AdminCommand
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -61,7 +62,7 @@ class ChatsCommand extends AdminCommand
null
,
//'yyyy-mm-dd hh:mm:ss' date range from
null
,
//'yyyy-mm-dd hh:mm:ss' date range to
null
,
//Specific chat_id to select
(
$text
===
''
||
$text
==
'*'
)
?
null
:
$text
//Text to search in user/group name
(
$text
===
''
||
$text
==
=
'*'
)
?
null
:
$text
//Text to search in user/group name
);
$user_chats
=
0
;
...
...
@@ -70,12 +71,13 @@ class ChatsCommand extends AdminCommand
if
(
$text
===
''
)
{
$text_back
=
''
;
}
elseif
(
$text
==
'*'
)
{
$text_back
=
'List of all bot chats:'
.
"
\n
"
;
}
elseif
(
$text
==
=
'*'
)
{
$text_back
=
'List of all bot chats:'
.
PHP_EOL
;
}
else
{
$text_back
=
'Chat search results:'
.
"
\n
"
;
$text_back
=
'Chat search results:'
.
PHP_EOL
;
}
if
(
is_array
(
$results
))
{
foreach
(
$results
as
$result
)
{
//Initialize a chat object
$result
[
'id'
]
=
$result
[
'chat_id'
];
...
...
@@ -88,36 +90,37 @@ class ChatsCommand extends AdminCommand
}
if
(
$chat
->
isPrivateChat
())
{
if
(
$text
!
=
''
)
{
$text_back
.=
'- P '
.
$chat
->
tryMention
()
.
' ['
.
$whois
.
']'
.
"
\n
"
;
if
(
$text
!=
=
''
)
{
$text_back
.=
'- P '
.
$chat
->
tryMention
()
.
' ['
.
$whois
.
']'
.
PHP_EOL
;
}
++
$user_chats
;
}
elseif
(
$chat
->
isSuperGroup
())
{
if
(
$text
!
=
''
)
{
$text_back
.=
'- S '
.
$chat
->
getTitle
()
.
' ['
.
$whois
.
']'
.
"
\n
"
;
if
(
$text
!=
=
''
)
{
$text_back
.=
'- S '
.
$chat
->
getTitle
()
.
' ['
.
$whois
.
']'
.
PHP_EOL
;
}
++
$super_group_chats
;
}
elseif
(
$chat
->
isGroupChat
())
{
if
(
$text
!
=
''
)
{
$text_back
.=
'- G '
.
$chat
->
getTitle
()
.
' ['
.
$whois
.
']'
.
"
\n
"
;
if
(
$text
!=
=
''
)
{
$text_back
.=
'- G '
.
$chat
->
getTitle
()
.
' ['
.
$whois
.
']'
.
PHP_EOL
;
}
++
$group_chats
;
}
}
}
if
((
$user_chats
+
$group_chats
+
$super_group_chats
)
===
0
)
{
$text_back
=
'No chats found..'
;
}
else
{
$text_back
.=
"
\n
"
.
'Private Chats: '
.
$user_chats
;
$text_back
.=
"
\n
"
.
'Group
: '
.
$group_chats
;
$text_back
.=
"
\n
"
.
'Super Group
: '
.
$super_group_chats
;
$text_back
.=
"
\n
"
.
'Total: '
.
(
$user_chats
+
$group_chats
+
$super_group_chats
);
$text_back
.=
PHP_EOL
.
'Private Chats: '
.
$user_chats
;
$text_back
.=
PHP_EOL
.
'Groups
: '
.
$group_chats
;
$text_back
.=
PHP_EOL
.
'Super Groups
: '
.
$super_group_chats
;
$text_back
.=
PHP_EOL
.
'Total: '
.
(
$user_chats
+
$group_chats
+
$super_group_chats
);
if
(
$text
===
''
)
{
$text_back
.=
"
\n\n
"
.
'List all chats: /'
.
$this
->
name
.
' *'
.
"
\n
"
.
'Search for chats: /'
.
$this
->
name
.
' <search string>'
;
$text_back
.=
PHP_EOL
.
PHP_EOL
.
'List all chats: /'
.
$this
->
name
.
' *'
.
PHP_EOL
.
'Search for chats: /'
.
$this
->
name
.
' <search string>'
;
}
}
...
...
src/Commands/AdminCommands/SendtoallCommand.php
View file @
818c3c84
...
...
@@ -11,6 +11,8 @@
namespace
Longman\TelegramBot\Commands\AdminCommands
;
use
Longman\TelegramBot\Commands\AdminCommand
;
use
Longman\TelegramBot\Entities\Message
;
use
Longman\TelegramBot\Entities\ServerResponse
;
use
Longman\TelegramBot\Request
;
/**
...
...
@@ -36,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/**
* @var string
*/
protected
$version
=
'1.
2.1
'
;
protected
$version
=
'1.
3.0
'
;
/**
* @var bool
...
...
@@ -47,6 +49,7 @@ class SendtoallCommand extends AdminCommand
* Execute command
*
* @return boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -68,18 +71,21 @@ class SendtoallCommand extends AdminCommand
null
//'yyyy-mm-dd hh:mm:ss' date range to
);
$tot
=
0
;
$fail
=
0
;
$tot
al
=
0
;
$fail
ed
=
0
;
$text
=
'Message sent to:'
.
"
\n
"
;
/** @var ServerResponse $result */
foreach
(
$results
as
$result
)
{
$
status
=
''
;
$
name
=
''
;
$type
=
''
;
if
(
$result
->
isOk
())
{
$status
=
'✔️'
;
$ServerResponse
=
$result
->
getResult
();
$chat
=
$ServerResponse
->
getChat
();
/** @var Message $message */
$message
=
$result
->
getResult
();
$chat
=
$message
->
getChat
();
if
(
$chat
->
isPrivateChat
())
{
$name
=
$chat
->
getFirstName
();
$type
=
'user'
;
...
...
@@ -89,15 +95,15 @@ class SendtoallCommand extends AdminCommand
}
}
else
{
$status
=
'✖️'
;
++
$fail
;
++
$fail
ed
;
}
++
$tot
;
++
$tot
al
;
$text
.=
$tot
.
') '
.
$status
.
' '
.
$type
.
' '
.
$name
.
"
\n
"
;
$text
.=
$tot
al
.
') '
.
$status
.
' '
.
$type
.
' '
.
$name
.
"
\n
"
;
}
$text
.=
'Delivered: '
.
(
$tot
-
$fail
)
.
'/'
.
$tot
.
"
\n
"
;
$text
.=
'Delivered: '
.
(
$tot
al
-
$failed
)
.
'/'
.
$total
.
"
\n
"
;
if
(
$tot
===
0
)
{
if
(
$tot
al
===
0
)
{
$text
=
'No users or chats found..'
;
}
}
...
...
src/Commands/AdminCommands/SendtochannelCommand.php
View file @
818c3c84
...
...
@@ -38,7 +38,7 @@ class SendtochannelCommand extends AdminCommand
/**
* @var string
*/
protected
$version
=
'0.
1.4
'
;
protected
$version
=
'0.
2.0
'
;
/**
* @var bool
...
...
@@ -56,36 +56,43 @@ class SendtochannelCommand extends AdminCommand
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse|mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$user_id
=
$message
->
getFrom
()
->
getId
();
$type
=
$message
->
getType
();
// 'Cast' the command type into message this protect the machine state
// if the commmad is recolled when the conversation is already started
$type
=
(
$type
==
'command'
)
?
'Message'
:
$type
;
// 'Cast' the command type into message to protect the machine state
// if the commmad is recalled when the conversation is already started
$type
=
(
$type
===
'command'
)
?
'Message'
:
$type
;
$text
=
trim
(
$message
->
getText
(
true
));
$text_yes_or_no
=
(
$text
===
'Yes'
||
$text
===
'No'
);
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$data
=
[
'chat_id'
=>
$chat_id
,
];
// Conversation
$this
->
conversation
=
new
Conversation
(
$user_id
,
$chat_id
,
$this
->
getName
());
$notes
=
&
$this
->
conversation
->
notes
;
$channels
=
(
array
)
$this
->
getConfig
(
'your_channel'
);
if
(
!
isset
(
$this
->
conversation
->
notes
[
'state'
]))
{
$state
=
(
count
(
$channels
)
==
0
)
?
-
1
:
0
;
$this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
if
(
isset
(
$notes
[
'state'
]))
{
$state
=
$notes
[
'state'
];
}
else
{
$state
=
$this
->
conversation
->
notes
[
'state'
];
$state
=
(
count
(
$channels
)
===
0
)
?
-
1
:
0
;
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
}
switch
(
$state
)
{
case
-
1
:
// getConfig has not been configured asking for channel to administer
if
(
$type
!=
'Message'
||
$text
===
''
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
-
1
;
if
(
$type
!=
=
'Message'
||
$text
===
''
)
{
$notes
[
'state'
]
=
-
1
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert the channel name: (@yourchannel)'
;
...
...
@@ -94,8 +101,8 @@ class SendtochannelCommand extends AdminCommand
break
;
}
$
this
->
conversation
->
notes
[
'channel'
]
=
$text
;
$
this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'channel'
]
=
$text
;
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
// Jump to state 1
goto
insert
;
...
...
@@ -103,38 +110,35 @@ class SendtochannelCommand extends AdminCommand
default
:
case
0
:
// getConfig has been configured choose channel
if
(
$type
!=
'Message'
||
!
in_array
(
$text
,
$channels
))
{
$
this
->
conversation
->
notes
[
'state'
]
=
0
;
if
(
$type
!=
=
'Message'
||
!
in_array
(
$text
,
$channels
,
true
))
{
$notes
[
'state'
]
=
0
;
$this
->
conversation
->
update
();
$keyboard
=
[];
foreach
(
$channels
as
$channel
)
{
$keyboard
[]
=
[
$channel
];
}
$
reply_keyboard_markup
=
new
ReplyKeyboardMarkup
(
$
data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
$keyboard
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
'selective'
=>
true
,
]
);
$data
[
'reply_markup'
]
=
$reply_keyboard_markup
;
$data
[
'text'
]
=
'Select a channel'
;
if
(
$type
!=
'Message'
||
!
in_array
(
$text
,
$channels
))
{
$data
[
'text'
]
=
'Select a channel from the keyboard:'
;
}
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$
this
->
conversation
->
notes
[
'channel'
]
=
$text
;
$
this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'channel'
]
=
$text
;
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
// no break
case
1
:
insert
:
if
(
$this
->
conversation
->
notes
[
'last_message_id'
]
==
$message
->
getMessageId
()
||
(
$type
==
'Message'
&&
$text
===
''
))
{
$
this
->
conversation
->
notes
[
'state'
]
=
1
;
if
(
(
$type
===
'Message'
&&
$text
===
''
)
||
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
(
))
{
$notes
[
'state'
]
=
1
;
$this
->
conversation
->
update
();
$data
[
'reply_markup'
]
=
new
ReplyKeyBoardHide
([
'selective'
=>
true
]);
...
...
@@ -142,45 +146,40 @@ class SendtochannelCommand extends AdminCommand
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$
this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$
this
->
conversation
->
notes
[
'message'
]
=
$message
->
reflect
();
$
this
->
conversation
->
notes
[
'message_type'
]
=
$type
;
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'message'
]
=
$message
->
reflect
();
$notes
[
'message_type'
]
=
$type
;
// no break
case
2
:
if
(
$this
->
conversation
->
notes
[
'last_message_id'
]
==
$message
->
getMessageId
()
||
!
(
$text
==
'Yes'
||
$text
==
'No'
))
{
$
this
->
conversation
->
notes
[
'state'
]
=
2
;
if
(
!
$text_yes_or_no
||
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
(
))
{
$notes
[
'state'
]
=
2
;
$this
->
conversation
->
update
();
// Execute this just with object that allow caption
if
(
$this
->
conversation
->
notes
[
'message_type'
]
==
'Video'
||
$this
->
conversation
->
notes
[
'message_type'
]
==
'Photo'
)
{
$keyboard
=
[[
'Yes'
,
'No'
]];
$reply_keyboard_markup
=
new
ReplyKeyboardMarkup
(
if
(
$notes
[
'message_type'
]
===
'Video'
||
$notes
[
'message_type'
]
===
'Photo'
)
{
$data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
$keyboard
,
'keyboard'
=>
[[
'Yes'
,
'No'
]]
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
'selective'
=>
true
,
]
);
$data
[
'reply_markup'
]
=
$reply_keyboard_markup
;
$data
[
'text'
]
=
'Would you
insert
caption?'
;
if
(
$this
->
conversation
->
notes
[
'last_message_id'
]
!=
$message
->
getMessageId
()
&&
!
(
$text
==
'Yes'
||
$text
==
'No'
))
{
$data
[
'text'
]
=
'Would you insert a caption?'
.
"
\n
"
.
'Type Yes or No'
;
$data
[
'text'
]
=
'Would you
like to insert a
caption?'
;
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
(
))
{
$data
[
'text'
]
.=
PHP_EOL
.
'Type Yes or No'
;
}
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
}
$this
->
conversation
->
notes
[
'set_caption'
]
=
false
;
if
(
$text
==
'Yes'
)
{
$this
->
conversation
->
notes
[
'set_caption'
]
=
true
;
}
$this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'set_caption'
]
=
(
$text
===
'Yes'
);
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
// no break
case
3
:
if
(
(
$this
->
conversation
->
notes
[
'last_message_id'
]
==
$message
->
getMessageId
()
||
$type
!=
'Message'
)
&&
$this
->
conversation
->
notes
[
'set_caption'
]
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
3
;
if
(
$notes
[
'set_caption'
]
&&
(
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
()
||
$type
!==
'Message'
)
)
{
$notes
[
'state'
]
=
3
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert caption:'
;
...
...
@@ -188,56 +187,52 @@ class SendtochannelCommand extends AdminCommand
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$
this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$
this
->
conversation
->
notes
[
'caption'
]
=
$text
;
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'caption'
]
=
$text
;
// no break
case
4
:
if
(
$this
->
conversation
->
notes
[
'last_message_id'
]
==
$message
->
getMessageId
()
||
!
(
$text
==
'Yes'
||
$text
==
'No'
))
{
$
this
->
conversation
->
notes
[
'state'
]
=
4
;
if
(
!
$text_yes_or_no
||
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
(
))
{
$notes
[
'state'
]
=
4
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Message will look like this:'
;
$result
=
Request
::
sendMessage
(
$data
);
if
(
$
this
->
conversation
->
notes
[
'message_type'
]
!
=
'command'
)
{
if
(
$
this
->
conversation
->
notes
[
'set_caption'
])
{
$data
[
'caption'
]
=
$
this
->
conversation
->
notes
[
'caption'
];
if
(
$
notes
[
'message_type'
]
!=
=
'command'
)
{
if
(
$notes
[
'set_caption'
])
{
$data
[
'caption'
]
=
$notes
[
'caption'
];
}
$
result
=
$this
->
sendBack
(
new
Message
(
$this
->
conversation
->
notes
[
'message'
],
'thisbot'
),
$data
);
$
this
->
sendBack
(
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotName
()
),
$data
);
$data
[
'text'
]
=
'Would you post it?'
;
if
(
$this
->
conversation
->
notes
[
'last_message_id'
]
!=
$message
->
getMessageId
()
&&
!
(
$text
==
'Yes'
||
$text
==
'No'
))
{
$data
[
'text'
]
=
'Would you post it?'
.
"
\n
"
.
'Press Yes or No'
;
}
$keyboard
=
[[
'Yes'
,
'No'
]];
$reply_keyboard_markup
=
new
ReplyKeyboardMarkup
(
$data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
$keyboard
,
'keyboard'
=>
[[
'Yes'
,
'No'
]]
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
'selective'
=>
true
,
]
);
$data
[
'reply_markup'
]
=
$reply_keyboard_markup
;
$data
[
'text'
]
=
'Would you like to post it?'
;
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
())
{
$data
[
'text'
]
.=
PHP_EOL
.
'Type Yes or No'
;
}
$result
=
Request
::
sendMessage
(
$data
);
}
break
;
}
$this
->
conversation
->
notes
[
'post_message'
]
=
false
;
if
(
$text
==
'Yes'
)
{
$this
->
conversation
->
notes
[
'post_message'
]
=
true
;
}
$this
->
conversation
->
notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'post_message'
]
=
(
$text
===
'Yes'
);
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
// no break
case
5
:
$data
[
'reply_markup'
]
=
new
ReplyKeyBoardHide
([
'selective'
=>
true
]);
if
(
$
this
->
conversation
->
notes
[
'post_message'
])
{
if
(
$notes
[
'post_message'
])
{
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$
this
->
conversation
->
notes
[
'message'
],
'anystring'
),
$
this
->
conversation
->
notes
[
'channel'
],
$
this
->
conversation
->
notes
[
'caption'
]
new
Message
(
$
notes
[
'message'
],
$this
->
telegram
->
getBotName
()
),
$notes
[
'channel'
],
$notes
[
'caption'
]
);
}
else
{
$data
[
'text'
]
=
'Abort by user, message not sent..'
;
...
...
@@ -246,31 +241,59 @@ class SendtochannelCommand extends AdminCommand
$this
->
conversation
->
stop
();
$result
=
Request
::
sendMessage
(
$data
);
}
return
$result
;
}
/**
*
Execute without db
*
SendBack
*
* @return mixed
* Received a message, the bot can send a copy of it to another chat/channel.
* You don't have to care about the type of the message, the function detect it and use the proper
* REQUEST:: function to send it.
* $data include all the var that you need to send the message to the proper chat
*
* @todo This method will be moved to a higher level maybe in AdminCommand or Command
* @todo Looking for a more significant name
*
* @param \Longman\TelegramBot\Entities\Message $message
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
p
ublic
function
executeNoDb
(
)
p
rotected
function
sendBack
(
Message
$message
,
array
$data
)
{
$message
=
$this
->
getMessage
();
$text
=
trim
(
$message
->
getText
(
true
));
$chat_id
=
$message
->
getChat
()
->
getId
();
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
if
(
$text
===
''
)
{
$data
[
'text'
]
=
'Usage: /sendtochannel <text>'
;
}
else
{
$channels
=
(
array
)
$this
->
getConfig
(
'your_channel'
);
$first_channel
=
$channels
[
0
];
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$message
->
reflect
(),
'anystring'
),
$first_channel
);
$type
=
$message
->
getType
();
$type
=
(
$type
===
'command'
)
?
'Message'
:
$type
;
if
(
$type
===
'Message'
)
{
$data
[
'text'
]
=
$message
->
getText
(
true
);
}
elseif
(
$type
===
'Audio'
)
{
$data
[
'audio'
]
=
$message
->
getAudio
()
->
getFileId
();
$data
[
'duration'
]
=
$message
->
getAudio
()
->
getDuration
();
$data
[
'performer'
]
=
$message
->
getAudio
()
->
getPerformer
();
$data
[
'title'
]
=
$message
->
getAudio
()
->
getTitle
();
}
elseif
(
$type
===
'Document'
)
{
$data
[
'document'
]
=
$message
->
getDocument
()
->
getFileId
();
}
elseif
(
$type
===
'Photo'
)
{
$data
[
'photo'
]
=
$message
->
getPhoto
()[
0
]
->
getFileId
();
}
elseif
(
$type
===
'Sticker'
)
{
$data
[
'sticker'
]
=
$message
->
getSticker
()
->
getFileId
();
}
elseif
(
$type
===
'Video'
)
{
$data
[
'video'
]
=
$message
->
getVideo
()
->
getFileId
();
}
elseif
(
$type
===
'Voice'
)
{
$data
[
'voice'
]
=
$message
->
getVoice
()
->
getFileId
();
}
elseif
(
$type
===
'Location'
)
{
$data
[
'latitude'
]
=
$message
->
getLocation
()
->
getLatitude
();
$data
[
'longitude'
]
=
$message
->
getLocation
()
->
getLongitude
();
}
return
Request
::
sendMessage
(
$data
);
$callback_path
=
'Longman\TelegramBot\Request'
;
$callback_function
=
'send'
.
$type
;
if
(
!
method_exists
(
$callback_path
,
$callback_function
))
{
throw
new
TelegramException
(
'Methods: '
.
$callback_function
.
' not found in class Request.'
);
}
return
call_user_func_array
(
$callback_path
.
'::'
.
$callback_function
,
[
$data
]);
}
/**
...
...
@@ -281,6 +304,7 @@ class SendtochannelCommand extends AdminCommand
* @param string|null $caption
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
protected
function
publish
(
Message
$message
,
$channel
,
$caption
=
null
)
{
...
...
@@ -292,61 +316,42 @@ class SendtochannelCommand extends AdminCommand
if
(
$this
->
sendBack
(
$message
,
$data
)
->
isOk
())
{
$response
=
'Message sent successfully to: '
.
$channel
;
}
else
{
$response
=
'Message not sent to: '
.
$channel
.
"
\n
"
.
'- Does the channel exist?'
.
"
\n
"
.
$response
=
'Message not sent to: '
.
$channel
.
PHP_EOL
.
'- Does the channel exist?'
.
PHP_EOL
.
'- Is the bot an admin of the channel?'
;
}
return
$response
;
}
/**
* SendBack
*
* Received a message, the bot can send a copy of it to another chat/channel.
* You don't have to care about the type of the message, the function detect it and use the proper
* REQUEST:: function to send it.
* $data include all the var that you need to send the message to the proper chat
* Execute without db
*
* @todo This method will be moved at an higher level maybe in AdminCommand or Command
* @todo Looking for a more significative name
* @todo Why send just to the first found channel?
*
* @param \Longman\TelegramBot\Entities\Message $message
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
p
rotected
function
sendBack
(
Message
$message
,
array
$data
)
p
ublic
function
executeNoDb
(
)
{
$type
=
$message
->
getType
();
$type
=
(
$type
==
'command'
)
?
'Message'
:
$type
;
if
(
$type
==
'Message'
)
{
$data
[
'text'
]
=
$message
->
getText
(
true
);
}
elseif
(
$type
==
'Audio'
)
{
$data
[
'audio'
]
=
$message
->
getAudio
()
->
getFileId
();
$data
[
'duration'
]
=
$message
->
getAudio
()
->
getDuration
();
$data
[
'performer'
]
=
$message
->
getAudio
()
->
getPerformer
();
$data
[
'title'
]
=
$message
->
getAudio
()
->
getTitle
();
}
elseif
(
$type
==
'Document'
)
{
$data
[
'document'
]
=
$message
->
getDocument
()
->
getFileId
();
}
elseif
(
$type
==
'Photo'
)
{
$data
[
'photo'
]
=
$message
->
getPhoto
()[
0
]
->
getFileId
();
}
elseif
(
$type
==
'Sticker'
)
{
$data
[
'sticker'
]
=
$message
->
getSticker
()
->
getFileId
();
}
elseif
(
$type
==
'Video'
)
{
$data
[
'video'
]
=
$message
->
getVideo
()
->
getFileId
();
}
elseif
(
$type
==
'Voice'
)
{
$data
[
'voice'
]
=
$message
->
getVoice
()
->
getFileId
();
}
elseif
(
$type
==
'Location'
)
{
$data
[
'latitude'
]
=
$message
->
getLocation
()
->
getLatitude
();
$data
[
'longitude'
]
=
$message
->
getLocation
()
->
getLongitude
();
}
$callback_path
=
'Longman\TelegramBot\Request'
;
$callback_function
=
'send'
.
$type
;
if
(
!
method_exists
(
$callback_path
,
$callback_function
))
{
throw
new
TelegramException
(
'Methods: '
.
$callback_function
.
' not found in class Request.'
);
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
trim
(
$message
->
getText
(
true
));
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
'Usage: '
.
$this
->
getUsage
(),
];
if
(
$text
!==
''
)
{
$channels
=
(
array
)
$this
->
getConfig
(
'your_channel'
);
$first_channel
=
$channels
[
0
];
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$message
->
reflect
(),
$this
->
telegram
->
getBotName
()),
$first_channel
);
}
return
call_user_func_array
(
$callback_path
.
'::'
.
$callback_function
,
[
$data
]
);
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/AdminCommands/WhoisCommand.php
View file @
818c3c84
...
...
@@ -15,6 +15,8 @@ namespace Longman\TelegramBot\Commands\AdminCommands;
use
Longman\TelegramBot\Commands\AdminCommand
;
use
Longman\TelegramBot\DB
;
use
Longman\TelegramBot\Entities\Chat
;
use
Longman\TelegramBot\Entities\PhotoSize
;
use
Longman\TelegramBot\Entities\UserProfilePhotos
;
use
Longman\TelegramBot\Request
;
/**
...
...
@@ -40,7 +42,7 @@ class WhoisCommand extends AdminCommand
/**
* @var string
*/
protected
$version
=
'1.
1
.0'
;
protected
$version
=
'1.
2
.0'
;
/**
* @var bool
...
...
@@ -51,6 +53,7 @@ class WhoisCommand extends AdminCommand
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -71,7 +74,7 @@ class WhoisCommand extends AdminCommand
$text
=
substr
(
$command
,
5
);
//We need that '-' now, bring it back
if
(
(
substr
(
$text
,
0
,
1
)
==
'g'
)
)
{
if
(
strpos
(
$text
,
'g'
)
===
0
)
{
$text
=
str_replace
(
'g'
,
'-'
,
$text
);
}
}
...
...
@@ -80,9 +83,13 @@ class WhoisCommand extends AdminCommand
$text
=
'Provide the id to lookup: /whois <id>'
;
}
else
{
$user_id
=
$text
;
$chat
=
null
;
$created_at
=
null
;
$updated_at
=
null
;
$result
=
null
;
if
(
is_numeric
(
$text
))
{
$result
=
DB
::
selectChats
(
$result
s
=
DB
::
selectChats
(
true
,
//Select groups (group chat)
true
,
//Select supergroups (super group chat)
true
,
//Select users (single chat)
...
...
@@ -91,7 +98,9 @@ class WhoisCommand extends AdminCommand
$user_id
//Specific chat_id to select
);
$result
=
$result
[
0
];
if
(
!
empty
(
$results
))
{
$result
=
reset
(
$results
);
}
}
else
{
$results
=
DB
::
selectChats
(
true
,
//Select groups (group chat)
...
...
@@ -103,8 +112,8 @@ class WhoisCommand extends AdminCommand
$text
//Text to search in user/group name
);
if
(
is_array
(
$results
)
&&
count
(
$results
)
==
1
)
{
$result
=
$results
[
0
]
;
if
(
is_array
(
$results
)
&&
count
(
$results
)
==
=
1
)
{
$result
=
reset
(
$results
)
;
}
}
...
...
@@ -118,36 +127,38 @@ class WhoisCommand extends AdminCommand
$old_id
=
$result
[
'old_id'
];
}
if
(
$chat
!=
null
)
{
if
(
$chat
!=
=
null
)
{
if
(
$chat
->
isPrivateChat
())
{
$text
=
'User ID: '
.
$user_id
.
"
\n
"
;
$text
.=
'Name: '
.
$chat
->
getFirstName
()
.
' '
.
$chat
->
getLastName
()
.
"
\n
"
;
$text
=
'User ID: '
.
$user_id
.
PHP_EOL
;
$text
.=
'Name: '
.
$chat
->
getFirstName
()
.
' '
.
$chat
->
getLastName
()
.
PHP_EOL
;
if
(
$chat
->
getUsername
()
!=
''
)
{
$text
.=
'Username: @'
.
$chat
->
getUsername
()
.
"
\n
"
;
$username
=
$chat
->
getUsername
();
if
(
$username
!==
null
&&
$username
!==
''
)
{
$text
.=
'Username: @'
.
$username
.
PHP_EOL
;
}
$text
.=
'First time seen: '
.
$created_at
.
"
\n
"
;
$text
.=
'Last activity: '
.
$updated_at
.
"
\n
"
;
$text
.=
'First time seen: '
.
$created_at
.
PHP_EOL
;
$text
.=
'Last activity: '
.
$updated_at
.
PHP_EOL
;
//Code from Whoami command
$limit
=
10
;
$offset
=
null
;
$ServerResponse
=
Request
::
getUserProfilePhotos
([
$response
=
Request
::
getUserProfilePhotos
(
[
'user_id'
=>
$user_id
,
'limit'
=>
$limit
,
'offset'
=>
$offset
,
]);
]
);
if
(
$
ServerR
esponse
->
isOk
())
{
$UserProfilePhoto
=
$ServerResponse
->
getResult
();
$
totalcount
=
$UserProfilePhoto
->
getTotalCoun
t
();
}
else
{
$totalcount
=
0
;
}
if
(
$
r
esponse
->
isOk
())
{
/** @var UserProfilePhotos $user_profile_photos */
$
user_profile_photos
=
$response
->
getResul
t
();
if
(
$user_profile_photos
->
getTotalCount
()
>
0
)
{
$photos
=
$user_profile_photos
->
getPhotos
();
if
(
$totalcount
>
0
)
{
$photos
=
$UserProfilePhoto
->
getPhotos
();
/** @var PhotoSize $photo */
$photo
=
$photos
[
0
][
2
];
$file_id
=
$photo
->
getFileId
();
...
...
@@ -156,12 +167,13 @@ class WhoisCommand extends AdminCommand
return
Request
::
sendPhoto
(
$data
);
}
}
}
elseif
(
$chat
->
isGroupChat
())
{
$text
=
'Chat ID: '
.
$user_id
.
(
!
empty
(
$old_id
)
?
' (previously: '
.
$old_id
.
')'
:
''
)
.
"
\n
"
;
$text
.=
'Type: '
.
ucfirst
(
$chat
->
getType
())
.
"
\n
"
;
$text
.=
'Title: '
.
$chat
->
getTitle
()
.
"
\n
"
;
$text
.=
'First time added to group: '
.
$created_at
.
"
\n
"
;
$text
.=
'Last activity: '
.
$updated_at
.
"
\n
"
;
$text
=
'Chat ID: '
.
$user_id
.
(
!
empty
(
$old_id
)
?
' (previously: '
.
$old_id
.
')'
:
''
)
.
PHP_EOL
;
$text
.=
'Type: '
.
ucfirst
(
$chat
->
getType
())
.
PHP_EOL
;
$text
.=
'Title: '
.
$chat
->
getTitle
()
.
PHP_EOL
;
$text
.=
'First time added to group: '
.
$created_at
.
PHP_EOL
;
$text
.=
'Last activity: '
.
$updated_at
.
PHP_EOL
;
}
}
elseif
(
is_array
(
$results
)
&&
count
(
$results
)
>
1
)
{
$text
=
'Multiple chats matched!'
;
...
...
@@ -171,6 +183,7 @@ class WhoisCommand extends AdminCommand
}
$data
[
'text'
]
=
$text
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/Command.php
View file @
818c3c84
...
...
@@ -20,7 +20,7 @@ abstract class Command
/**
* Telegram object
*
* @var Telegram
* @var
\Longman\TelegramBot\
Telegram
*/
protected
$telegram
;
...
...
@@ -90,7 +90,7 @@ abstract class Command
/**
* Constructor
*
* @param
Telegram
$telegram
* @param
\Longman\TelegramBot\Telegram
$telegram
* @param \Longman\TelegramBot\Entities\Update $update
*/
public
function
__construct
(
Telegram
$telegram
,
Update
$update
=
null
)
...
...
@@ -104,14 +104,16 @@ abstract class Command
* Set update object
*
* @param \Longman\TelegramBot\Entities\Update $update
* @return Command
*
* @return \Longman\TelegramBot\Commands\Command
*/
public
function
setUpdate
(
Update
$update
=
null
)
{
if
(
!
empty
(
$update
)
)
{
if
(
$update
!==
null
)
{
$this
->
update
=
$update
;
$this
->
message
=
$this
->
update
->
getMessage
();
}
return
$this
;
}
...
...
@@ -119,12 +121,14 @@ abstract class Command
* Pre-execute command
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
preExecute
()
{
if
(
$this
->
need_mysql
&&
!
(
$this
->
telegram
->
isDbEnabled
()
&&
DB
::
isDbConnected
()))
{
return
$this
->
executeNoDb
();
}
return
$this
->
execute
();
}
...
...
@@ -132,6 +136,7 @@ abstract class Command
* Execute command
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
abstract
public
function
execute
();
...
...
@@ -139,6 +144,7 @@ abstract class Command
* Execution if MySQL is required but not available
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
executeNoDb
()
{
...
...
@@ -182,7 +188,7 @@ abstract class Command
*
* @param string|null $name
*
* @return
mixed
* @return
array|mixed|null
*/
public
function
getConfig
(
$name
=
null
)
{
...
...
@@ -192,13 +198,14 @@ abstract class Command
if
(
isset
(
$this
->
config
[
$name
]))
{
return
$this
->
config
[
$name
];
}
return
null
;
}
/**
* Get telegram object
*
* @return Telegram
* @return
\Longman\TelegramBot\
Telegram
*/
public
function
getTelegram
()
{
...
...
@@ -248,7 +255,7 @@ abstract class Command
/**
* Check if command is enabled
*
* @return bool
ean
* @return bool
*/
public
function
isEnabled
()
{
...
...
src/Commands/SystemCommand.php
View file @
818c3c84
...
...
@@ -24,7 +24,7 @@ abstract class SystemCommand extends Command
*/
public
function
execute
()
{
//System command, return empty ServerResponse
//System command, return empty ServerResponse
by default
return
Request
::
emptyResponse
();
}
}
src/Commands/SystemCommands/CallbackqueryCommand.php
View file @
818c3c84
...
...
@@ -31,12 +31,13 @@ class CallbackqueryCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0
.0'
;
protected
$version
=
'1.
1
.0'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -45,15 +46,11 @@ class CallbackqueryCommand extends SystemCommand
$callback_query_id
=
$callback_query
->
getId
();
$callback_data
=
$callback_query
->
getData
();
$data
[
'callback_query_id'
]
=
$callback_query_id
;
if
(
$callback_data
==
'thumb up'
)
{
$data
[
'text'
]
=
'Hello World!'
;
$data
[
'show_alert'
]
=
true
;
}
else
{
$data
[
'text'
]
=
'Hello World!'
;
$data
[
'show_alert'
]
=
false
;
}
$data
=
[
'callback_query_id'
=>
$callback_query_id
,
'text'
=>
'Hello World!'
,
'show_alert'
=>
$callback_data
===
'thumb up'
,
];
return
Request
::
answerCallbackQuery
(
$data
);
}
...
...
src/Commands/SystemCommands/ChannelchatcreatedCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class ChannelchatcreatedCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.0.1
'
;
protected
$version
=
'1.1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$channel_chat_created = $message->getChannelChatCreated();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/ChoseninlineresultCommand.php
View file @
818c3c84
...
...
@@ -30,13 +30,21 @@ class ChoseninlineresultCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//Information about chosen result is returned
//$update = $this->getUpdate();
//$inline_query = $update->getChosenInlineResult();
//$query = $inline_query->getQuery();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/DeletechatphotoCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class DeletechatphotoCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$delete_chat_photo = $message->getDeleteChatPhoto();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/EditedmessageCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class EditedmessageCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0
.0'
;
protected
$version
=
'1.
1
.0'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
$update = $this->getUpdate();
$edited_message = $update->getEditedMessage();
}*/
//$update = $this->getUpdate();
//$edited_message = $update->getEditedMessage();
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/GenericCommand.php
View file @
818c3c84
...
...
@@ -31,12 +31,13 @@ class GenericCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -47,8 +48,9 @@ class GenericCommand extends SystemCommand
$user_id
=
$message
->
getFrom
()
->
getId
();
$command
=
$message
->
getCommand
();
if
(
in_array
(
$user_id
,
$this
->
telegram
->
getAdminList
())
&&
strtolower
(
substr
(
$command
,
0
,
5
))
==
'whois'
)
{
return
$this
->
telegram
->
executeCommand
(
'whois'
,
$this
->
update
);
//If the user is and admin and the command is in the format "/whoisXYZ", call the /whois command
if
(
stripos
(
$command
,
'whois'
)
===
0
&&
$this
->
telegram
->
isAdmin
(
$user_id
))
{
return
$this
->
telegram
->
executeCommand
(
'whois'
);
}
$data
=
[
...
...
src/Commands/SystemCommands/GenericmessageCommand.php
View file @
818c3c84
...
...
@@ -32,7 +32,7 @@ class GenericmessageCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.2
'
;
protected
$version
=
'1.
1.0
'
;
/**
* @var bool
...
...
@@ -54,6 +54,7 @@ class GenericmessageCommand extends SystemCommand
* Execute command
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -64,7 +65,7 @@ class GenericmessageCommand extends SystemCommand
);
//Fetch conversation command if it exists and execute it
if
(
$conversation
->
exists
()
&&
(
$command
=
$conversation
->
getCommand
()))
{
return
$this
->
telegram
->
executeCommand
(
$command
,
$this
->
update
);
return
$this
->
telegram
->
executeCommand
(
$command
);
}
return
Request
::
emptyResponse
();
...
...
src/Commands/SystemCommands/GroupchatcreatedCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class GroupchatcreatedCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$group_chat_created = $message->getGroupChatCreated();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/InlinequeryCommand.php
View file @
818c3c84
...
...
@@ -33,12 +33,13 @@ class InlinequeryCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.2
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -55,19 +56,19 @@ class InlinequeryCommand extends SystemCommand
'id'
=>
'001'
,
'title'
=>
'https://core.telegram.org/bots/api#answerinlinequery'
,
'description'
=>
'you enter: '
.
$query
,
'input_message_content'
=>
new
InputTextMessageContent
([
'message_text'
=>
' '
.
$query
])
'input_message_content'
=>
new
InputTextMessageContent
([
'message_text'
=>
' '
.
$query
])
,
],
[
'id'
=>
'002'
,
'title'
=>
'https://core.telegram.org/bots/api#answerinlinequery'
,
'description'
=>
'you enter: '
.
$query
,
'input_message_content'
=>
new
InputTextMessageContent
([
'message_text'
=>
' '
.
$query
])
'input_message_content'
=>
new
InputTextMessageContent
([
'message_text'
=>
' '
.
$query
])
,
],
[
'id'
=>
'003'
,
'title'
=>
'https://core.telegram.org/bots/api#answerinlinequery'
,
'description'
=>
'you enter: '
.
$query
,
'input_message_content'
=>
new
InputTextMessageContent
([
'message_text'
=>
' '
.
$query
])
'input_message_content'
=>
new
InputTextMessageContent
([
'message_text'
=>
' '
.
$query
])
,
],
];
...
...
src/Commands/SystemCommands/LeftchatmemberCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class LeftchatmemberCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$member = $message->getLeftChatMember();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/MigratefromchatidCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class MigratefromchatidCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$migrate_from_chat_id = $message->getMigrateFromChatId();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/MigratetochatidCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class MigratetochatidCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$migrate_to_chat_id = $message->getMigrateToChatId();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/NewchatmemberCommand.php
View file @
818c3c84
...
...
@@ -31,10 +31,13 @@ class NewchatmemberCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -42,10 +45,9 @@ class NewchatmemberCommand extends SystemCommand
$chat_id
=
$message
->
getChat
()
->
getId
();
$member
=
$message
->
getNewChatMember
();
if
(
$message
->
botAddedInChat
())
{
$text
=
'Hi there!'
;
}
else
{
if
(
!
$message
->
botAddedInChat
())
{
$text
=
'Hi '
.
$member
->
tryMention
()
.
'!'
;
}
...
...
src/Commands/SystemCommands/NewchatphotoCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class NewchatphotoCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$new_chat_photo = $message->getNewChatPhoto();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/NewchattitleCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class NewchattitleCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$new_chat_title = $message->getNewChatTitle();
}*/
return
parent
::
execute
();
}
}
src/Commands/SystemCommands/StartCommand.php
View file @
818c3c84
...
...
@@ -36,19 +36,20 @@ class StartCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
'Hi there!'
.
"
\n
"
.
'Type /help to see all commands!'
;
$text
=
'Hi there!'
.
PHP_EOL
.
'Type /help to see all commands!'
;
$data
=
[
'chat_id'
=>
$chat_id
,
...
...
src/Commands/SystemCommands/SupergroupchatcreatedCommand.php
View file @
818c3c84
...
...
@@ -30,11 +30,19 @@ class SupergroupchatcreatedCommand extends SystemCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/*public function execute()
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
//$message = $this->getMessage();
//$supergroup_chat_created = $message->getSuperGroupChatCreated();
}*/
return
parent
::
execute
();
}
}
src/Commands/UserCommands/CancelCommand.php
View file @
818c3c84
...
...
@@ -42,7 +42,7 @@ class CancelCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'0.
1.1
'
;
protected
$version
=
'0.
2.0
'
;
/**
* @var bool
...
...
@@ -53,6 +53,7 @@ class CancelCommand extends UserCommand
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -72,22 +73,13 @@ class CancelCommand extends UserCommand
return
$this
->
hideKeyboard
(
$text
);
}
/**
* Execute no db
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public
function
executeNoDb
()
{
return
$this
->
hideKeyboard
(
'Nothing to cancel.'
);
}
/**
* Hide the keyboard and output a text
*
* @param string $text
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
function
hideKeyboard
(
$text
)
{
...
...
@@ -99,4 +91,15 @@ class CancelCommand extends UserCommand
]
);
}
/**
* Execute no db
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
executeNoDb
()
{
return
$this
->
hideKeyboard
(
'Nothing to cancel.'
);
}
}
src/Commands/UserCommands/DateCommand.php
View file @
818c3c84
...
...
@@ -10,11 +10,13 @@
namespace
Longman\TelegramBot\Commands\UserCommands
;
use
DateTime
;
use
DateTimeZone
;
use
GuzzleHttp\Client
;
use
GuzzleHttp\Exception\RequestException
;
use
Longman\TelegramBot\Commands\UserCommand
;
use
Longman\TelegramBot\Exception\TelegramException
;
use
Longman\TelegramBot\Request
;
use
Longman\TelegramBot\TelegramLog
;
/**
* User "/date" command
...
...
@@ -39,7 +41,7 @@ class DateCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'1.
3
.0'
;
protected
$version
=
'1.
4
.0'
;
/**
* Guzzle Client object
...
...
@@ -74,8 +76,7 @@ class DateCommand extends UserCommand
*
* @param string $location
*
* @return array|boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
* @return array
*/
private
function
getCoordinates
(
$location
)
{
...
...
@@ -89,14 +90,16 @@ class DateCommand extends UserCommand
try
{
$response
=
$this
->
client
->
get
(
$path
,
[
'query'
=>
$query
]);
}
catch
(
RequestException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
TelegramLog
::
error
(
$e
->
getMessage
());
return
[];
}
if
(
!
(
$
result
=
$this
->
validateResponseData
(
$response
->
getBody
())))
{
return
false
;
if
(
!
(
$
data
=
$this
->
validateResponseData
(
$response
->
getBody
())))
{
return
[]
;
}
$result
=
$
result
[
'results'
][
0
];
$result
=
$
data
[
'results'
][
0
];
$lat
=
$result
[
'geometry'
][
'location'
][
'lat'
];
$lng
=
$result
[
'geometry'
][
'location'
][
'lng'
];
$acc
=
$result
[
'geometry'
][
'location_type'
];
...
...
@@ -111,8 +114,7 @@ class DateCommand extends UserCommand
* @param string $lat
* @param string $lng
*
* @return array|boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
* @return array
*/
private
function
getDate
(
$lat
,
$lng
)
{
...
...
@@ -123,7 +125,7 @@ class DateCommand extends UserCommand
$query
=
[
'location'
=>
urlencode
(
$lat
)
.
','
.
urlencode
(
$lng
),
'timestamp'
=>
urlencode
(
$timestamp
)
'timestamp'
=>
urlencode
(
$timestamp
)
,
];
if
(
$this
->
google_api_key
!==
null
)
{
...
...
@@ -133,16 +135,18 @@ class DateCommand extends UserCommand
try
{
$response
=
$this
->
client
->
get
(
$path
,
[
'query'
=>
$query
]);
}
catch
(
RequestException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
TelegramLog
::
error
(
$e
->
getMessage
());
return
[];
}
if
(
!
(
$
result
=
$this
->
validateResponseData
(
$response
->
getBody
())))
{
return
false
;
if
(
!
(
$
data
=
$this
->
validateResponseData
(
$response
->
getBody
())))
{
return
[]
;
}
$local_time
=
$timestamp
+
$
result
[
'rawOffset'
]
+
$result
[
'dstOffset'
];
$local_time
=
$timestamp
+
$
data
[
'rawOffset'
]
+
$data
[
'dstOffset'
];
return
[
$local_time
,
$
result
[
'timeZoneId'
]];
return
[
$local_time
,
$
data
[
'timeZoneId'
]];
}
/**
...
...
@@ -150,48 +154,48 @@ class DateCommand extends UserCommand
*
* @param string $data
*
* @return
bool|
array
* @return array
*/
private
function
validateResponseData
(
$data
)
{
if
(
empty
(
$data
))
{
return
false
;
return
[]
;
}
$data
=
json_decode
(
$data
,
true
);
if
(
empty
(
$data
))
{
return
false
;
return
[]
;
}
if
(
isset
(
$data
[
'status'
])
&&
$data
[
'status'
]
!==
'OK'
)
{
return
false
;
return
[]
;
}
return
$data
;
}
/**
* Get formatted date
* Get formatted date
at the passed location
*
* @param string $location
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
function
getFormattedDate
(
$location
)
{
if
(
empty
(
$location
)
)
{
if
(
$location
===
null
||
$location
===
''
)
{
return
'The time in nowhere is never'
;
}
list
(
$lat
,
$lng
,
$acc
,
$types
)
=
$this
->
getCoordinates
(
$location
);
list
(
$lat
,
$lng
)
=
$this
->
getCoordinates
(
$location
);
if
(
empty
(
$lat
)
||
empty
(
$lng
))
{
return
'It seems that in "'
.
$location
.
'" they do not have a concept of time.'
;
}
list
(
$local_time
,
$timezone_id
)
=
$this
->
getDate
(
$lat
,
$lng
);
$date_utc
=
new
\DateTime
(
gmdate
(
'Y-m-d H:i:s'
,
$local_time
),
new
\
DateTimeZone
(
$timezone_id
));
$date_utc
=
new
DateTime
(
gmdate
(
'Y-m-d H:i:s'
,
$local_time
),
new
DateTimeZone
(
$timezone_id
));
return
'The local time in '
.
$timezone_id
.
' is: '
.
$date_utc
->
format
(
$this
->
date_format
);
}
...
...
@@ -200,6 +204,7 @@ class DateCommand extends UserCommand
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -214,9 +219,9 @@ class DateCommand extends UserCommand
$chat_id
=
$message
->
getChat
()
->
getId
();
$location
=
$message
->
getText
(
true
);
if
(
empty
(
$location
))
{
$text
=
'You must specify location in format: /date <city>'
;
}
else
{
if
(
$location
!==
''
)
{
$text
=
$this
->
getFormattedDate
(
$location
);
}
...
...
src/Commands/UserCommands/EchoCommand.php
View file @
818c3c84
...
...
@@ -36,12 +36,13 @@ class EchoCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
src/Commands/UserCommands/HelpCommand.php
View file @
818c3c84
...
...
@@ -10,6 +10,7 @@
namespace
Longman\TelegramBot\Commands\UserCommands
;
use
Longman\TelegramBot\Commands\Command
;
use
Longman\TelegramBot\Commands\UserCommand
;
use
Longman\TelegramBot\Request
;
...
...
@@ -36,12 +37,13 @@ class HelpCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -52,26 +54,43 @@ class HelpCommand extends UserCommand
$command
=
trim
(
$message
->
getText
(
true
));
//Only get enabled Admin and User commands
$commands
=
array_filter
(
$this
->
telegram
->
getCommandsList
(),
function
(
$command
)
{
return
(
!
$command
->
isSystemCommand
()
&&
$command
->
isEnabled
());
/** @var Command[] $command_objs */
$command_objs
=
array_filter
(
$this
->
telegram
->
getCommandsList
(),
function
(
$command_obj
)
{
/** @var Command $command_obj */
return
!
$command_obj
->
isSystemCommand
()
&&
$command_obj
->
isEnabled
();
});
//If no command parameter is passed, show the list
if
(
$command
===
''
)
{
$text
=
$this
->
telegram
->
getBotName
()
.
' v. '
.
$this
->
telegram
->
getVersion
()
.
"
\n\n
"
;
$text
.=
'Commands List:'
.
"
\n
"
;
foreach
(
$commands
as
$command
)
{
$text
.=
'/'
.
$command
->
getName
()
.
' - '
.
$command
->
getDescription
()
.
"
\n
"
;
$text
=
sprintf
(
'%s v. %s'
.
PHP_EOL
.
PHP_EOL
.
'Commands List:'
.
PHP_EOL
,
$this
->
telegram
->
getBotName
(),
$this
->
telegram
->
getVersion
()
);
foreach
(
$command_objs
as
$command
)
{
$text
.=
sprintf
(
'/%s - %s'
.
PHP_EOL
,
$command
->
getName
(),
$command
->
getDescription
()
);
}
$text
.=
"
\n
"
.
'For exact command help type: /help <command>'
;
$text
.=
PHP_EOL
.
'For exact command help type: /help <command>'
;
}
else
{
$command
=
str_replace
(
'/'
,
''
,
$command
);
if
(
isset
(
$commands
[
$command
]))
{
$command
=
$commands
[
$command
];
$text
=
'Command: '
.
$command
->
getName
()
.
' v'
.
$command
->
getVersion
()
.
"
\n
"
;
$text
.=
'Description: '
.
$command
->
getDescription
()
.
"
\n
"
;
$text
.=
'Usage: '
.
$command
->
getUsage
();
if
(
isset
(
$command_objs
[
$command
]))
{
/** @var Command $command_obj */
$command_obj
=
$command_objs
[
$command
];
$text
=
sprintf
(
'Command: %s v%s'
.
PHP_EOL
.
'Description: %s'
.
PHP_EOL
.
'Usage: %s'
,
$command_obj
->
getName
(),
$command_obj
->
getVersion
(),
$command_obj
->
getDescription
(),
$command_obj
->
getUsage
()
);
}
else
{
$text
=
'No help available: Command /'
.
$command
.
' not found'
;
}
...
...
src/Commands/UserCommands/SlapCommand.php
View file @
818c3c84
...
...
@@ -36,19 +36,19 @@ class SlapCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'1.
0.1
'
;
protected
$version
=
'1.
1.0
'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$message_id
=
$message
->
getMessageId
();
$text
=
$message
->
getText
(
true
);
$sender
=
'@'
.
$message
->
getFrom
()
->
getUsername
();
...
...
src/Commands/UserCommands/SurveyCommand.php
View file @
818c3c84
...
...
@@ -10,6 +10,7 @@
namespace
Longman\TelegramBot\Commands\UserCommands
;
use
Longman\TelegramBot\Entities\PhotoSize
;
use
Longman\TelegramBot\Request
;
use
Longman\TelegramBot\Conversation
;
use
Longman\TelegramBot\Commands\UserCommand
;
...
...
@@ -40,7 +41,7 @@ class SurveyCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'0.
2
.0'
;
protected
$version
=
'0.
3
.0'
;
/**
* @var bool
...
...
@@ -58,6 +59,7 @@ class SurveyCommand extends UserCommand
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -65,186 +67,206 @@ class SurveyCommand extends UserCommand
$chat
=
$message
->
getChat
();
$user
=
$message
->
getFrom
();
$text
=
$message
->
getText
(
true
);
$text
=
trim
(
$message
->
getText
(
true
)
);
$chat_id
=
$chat
->
getId
();
$user_id
=
$user
->
getId
();
//Preparing Respose
$data
=
[];
//Preparing Response
$data
=
[
'chat_id'
=>
$chat_id
,
];
if
(
$chat
->
isGroupChat
()
||
$chat
->
isSuperGroup
())
{
//reply to message id is applied by default
//Force reply is applied by default
to so
can work with privacy on
//Force reply is applied by default
so it
can work with privacy on
$data
[
'reply_markup'
]
=
new
ForceReply
([
'selective'
=>
true
]);
}
$data
[
'chat_id'
]
=
$chat_id
;
//Conversation start
$this
->
conversation
=
new
Conversation
(
$user_id
,
$chat_id
,
$this
->
getName
());
$notes
=
&
$this
->
conversation
->
notes
;
//cache data from the tracking session if any
if
(
!
isset
(
$this
->
conversation
->
notes
[
'state'
]))
{
$state
=
'0'
;
}
else
{
$state
=
$this
->
conversation
->
notes
[
'state'
];
$state
=
0
;
if
(
isset
(
$notes
[
'state'
]))
{
$state
=
$notes
[
'state'
];
}
//state machine
//entrypoint of the machine state if given by the track
//Every time the step is achived the track is updated
$result
=
Request
::
emptyResponse
();
//State machine
//Entrypoint of the machine state if given by the track
//Every time a step is achieved the track is updated
switch
(
$state
)
{
case
0
:
if
(
empty
(
$text
)
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
0
;
if
(
$text
===
''
)
{
$notes
[
'state'
]
=
0
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Type your name:'
;
$data
[
'reply_markup'
]
=
new
ReplyKeyBoardHide
([
'selective'
=>
true
]);
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$this
->
conversation
->
notes
[
'name'
]
=
$text
;
$notes
[
'name'
]
=
$text
;
$text
=
''
;
// no break
case
1
:
if
(
empty
(
$text
)
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
1
;
if
(
$text
===
''
)
{
$notes
[
'state'
]
=
1
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Type your surname:'
;
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$this
->
conversation
->
notes
[
'surname'
]
=
$text
;
++
$state
;
$notes
[
'surname'
]
=
$text
;
$text
=
''
;
// no break
case
2
:
if
(
empty
(
$text
)
||
!
is_numeric
(
$text
))
{
$
this
->
conversation
->
notes
[
'state'
]
=
2
;
if
(
$text
===
''
||
!
is_numeric
(
$text
))
{
$notes
[
'state'
]
=
2
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Type your age:'
;
if
(
!
empty
(
$text
)
&&
!
is_numeric
(
$text
)
)
{
$data
[
'text'
]
=
'Type your age, must be a number'
;
if
(
$text
!==
''
)
{
$data
[
'text'
]
=
'Type your age, must be a number
:
'
;
}
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$this
->
conversation
->
notes
[
'age'
]
=
$text
;
$notes
[
'age'
]
=
$text
;
$text
=
''
;
// no break
case
3
:
if
(
empty
(
$text
)
||
!
(
$text
==
'M'
||
$text
==
'F'
))
{
$
this
->
conversation
->
notes
[
'state'
]
=
3
;
if
(
$text
===
''
||
!
(
$text
===
'M'
||
$text
=
==
'F'
))
{
$notes
[
'state'
]
=
3
;
$this
->
conversation
->
update
();
$keyboard
=
[[
'M'
,
'F'
]];
$reply_keyboard_markup
=
new
ReplyKeyboardMarkup
(
$data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
$keyboard
,
'keyboard'
=>
[[
'M'
,
'F'
]]
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
'selective'
=>
true
,
]
);
$data
[
'reply_markup'
]
=
$reply_keyboard_markup
;
$data
[
'text'
]
=
'Select your gender:'
;
if
(
!
empty
(
$text
)
&&
!
(
$text
==
'M'
||
$text
==
'F'
)
)
{
if
(
$text
!==
''
)
{
$data
[
'text'
]
=
'Select your gender, choose a keyboard option:'
;
}
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$this
->
conversation
->
notes
[
'gender'
]
=
$text
;
$text
=
''
;
$notes
[
'gender'
]
=
$text
;
// no break
case
4
:
if
(
is_null
(
$message
->
getLocation
())
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
4
;
if
(
$message
->
getLocation
()
===
null
)
{
$notes
[
'state'
]
=
4
;
$this
->
conversation
->
update
();
$data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
[
[
[
'text'
=>
'Share Location'
,
'request_location'
=>
true
'request_location'
=>
true
,
],
],
]
],
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
$data
[
'text'
]
=
'Share your location:'
;
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$
this
->
conversation
->
notes
[
'longitude'
]
=
$message
->
getLocation
()
->
getLongitude
();
$
this
->
conversation
->
notes
[
'latitude'
]
=
$message
->
getLocation
()
->
getLatitude
();
$notes
[
'longitude'
]
=
$message
->
getLocation
()
->
getLongitude
();
$notes
[
'latitude'
]
=
$message
->
getLocation
()
->
getLatitude
();
// no break
case
5
:
if
(
is_null
(
$message
->
getPhoto
())
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
5
;
if
(
$message
->
getPhoto
()
===
null
)
{
$notes
[
'state'
]
=
5
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert your picture:'
;
$data
[
'reply_markup'
]
=
new
ReplyKeyBoardHide
([
'selective'
=>
true
]);
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$this
->
conversation
->
notes
[
'photo_id'
]
=
$message
->
getPhoto
()[
0
]
->
getFileId
();
/** @var PhotoSize $photo */
$photo
=
$message
->
getPhoto
()[
0
];
$notes
[
'photo_id'
]
=
$photo
->
getFileId
();
// no break
case
6
:
if
(
is_null
(
$message
->
getContact
())
)
{
$
this
->
conversation
->
notes
[
'state'
]
=
6
;
if
(
$message
->
getContact
()
===
null
)
{
$notes
[
'state'
]
=
6
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Share your contact information:'
;
$data
[
'reply_markup'
]
=
new
ReplyKeyboardMarkup
(
[
'keyboard'
=>
[
[
[
'text'
=>
'Share Contact'
,
'request_contact'
=>
true
'request_contact'
=>
true
,
],
],
]
],
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
$data
[
'text'
]
=
'Share your contact information:'
;
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$this
->
conversation
->
notes
[
'phone_number'
]
=
$message
->
getContact
()
->
getPhoneNumber
();
$notes
[
'phone_number'
]
=
$message
->
getContact
()
->
getPhoneNumber
();
// no break
case
7
:
$this
->
conversation
->
update
();
$out_text
=
'/Survey result:'
.
"
\n
"
;
unset
(
$
this
->
conversation
->
notes
[
'state'
]);
foreach
(
$
this
->
conversation
->
notes
as
$k
=>
$v
)
{
$out_text
.=
"
\n
"
.
ucfirst
(
$k
)
.
': '
.
$v
;
$out_text
=
'/Survey result:'
.
PHP_EOL
;
unset
(
$notes
[
'state'
]);
foreach
(
$notes
as
$k
=>
$v
)
{
$out_text
.=
PHP_EOL
.
ucfirst
(
$k
)
.
': '
.
$v
;
}
$data
[
'photo'
]
=
$
this
->
conversation
->
notes
[
'photo_id'
];
$data
[
'photo'
]
=
$notes
[
'photo_id'
];
$data
[
'reply_markup'
]
=
new
ReplyKeyBoardHide
([
'selective'
=>
true
]);
$data
[
'caption'
]
=
$out_text
;
$this
->
conversation
->
stop
();
$result
=
Request
::
sendPhoto
(
$data
);
break
;
}
return
$result
;
}
}
src/Commands/UserCommands/WeatherCommand.php
View file @
818c3c84
...
...
@@ -10,11 +10,12 @@
namespace
Longman\TelegramBot\Commands\UserCommands
;
use
Exception
;
use
GuzzleHttp\Client
;
use
GuzzleHttp\Exception\RequestException
;
use
Longman\TelegramBot\Commands\UserCommand
;
use
Longman\TelegramBot\Exception\TelegramException
;
use
Longman\TelegramBot\Request
;
use
Longman\TelegramBot\TelegramLog
;
/**
* User "/weather" command
...
...
@@ -39,7 +40,7 @@ class WeatherCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'1.
1
.0'
;
protected
$version
=
'1.
2
.0'
;
/**
* Base URI for OpenWeatherMap API
...
...
@@ -54,7 +55,6 @@ class WeatherCommand extends UserCommand
* @param string $location
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
function
getWeatherData
(
$location
)
{
...
...
@@ -69,7 +69,9 @@ class WeatherCommand extends UserCommand
try
{
$response
=
$client
->
get
(
$path
,
[
'query'
=>
$query
]);
}
catch
(
RequestException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
TelegramLog
::
error
(
$e
->
getMessage
());
return
''
;
}
return
(
string
)
$response
->
getBody
();
...
...
@@ -80,13 +82,13 @@ class WeatherCommand extends UserCommand
*
* @param array $data
*
* @return
bool|
string
* @return string
*/
private
function
getWeatherString
(
array
$data
)
{
try
{
if
(
empty
(
$data
)
||
$data
[
'cod'
]
!==
200
)
{
return
false
;
if
(
!
(
isset
(
$data
[
'cod'
])
&&
$data
[
'cod'
]
===
200
)
)
{
return
''
;
}
//http://openweathermap.org/weather-conditions
...
...
@@ -101,16 +103,18 @@ class WeatherCommand extends UserCommand
$conditions_now
=
strtolower
(
$data
[
'weather'
][
0
][
'main'
]);
return
sprintf
(
'The temperature in %
1$s (%2$s) is %3$
s°C'
.
"
\n
"
.
'Current conditions are: %
4$s%5$
s'
,
'The temperature in %
s (%s) is %
s°C'
.
"
\n
"
.
'Current conditions are: %
s%
s'
,
$data
[
'name'
],
//city
$data
[
'sys'
][
'country'
],
//country
$data
[
'main'
][
'temp'
],
//temperature
$data
[
'weather'
][
0
][
'description'
],
//description of weather
(
isset
(
$conditions
[
$conditions_now
])
)
?
$conditions
[
$conditions_now
]
:
''
isset
(
$conditions
[
$conditions_now
]
)
?
$conditions
[
$conditions_now
]
:
''
);
}
catch
(
\Exception
$e
)
{
return
false
;
}
catch
(
Exception
$e
)
{
TelegramLog
::
error
(
$e
->
getMessage
());
return
''
;
}
}
...
...
@@ -118,6 +122,7 @@ class WeatherCommand extends UserCommand
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
...
...
@@ -126,11 +131,12 @@ class WeatherCommand extends UserCommand
$text
=
''
;
if
(
trim
(
$this
->
getConfig
(
'owm_api_key'
)))
{
if
(
$location
=
trim
(
$message
->
getText
(
true
)))
{
$location
=
trim
(
$message
->
getText
(
true
));
if
(
$location
!==
''
)
{
if
(
$weather_data
=
json_decode
(
$this
->
getWeatherData
(
$location
),
true
))
{
$text
=
$this
->
getWeatherString
(
$weather_data
);
}
if
(
!
$text
)
{
if
(
$text
===
''
)
{
$text
=
'Cannot find weather for location: '
.
$location
;
}
}
else
{
...
...
src/Commands/UserCommands/WhoamiCommand.php
View file @
818c3c84
...
...
@@ -14,6 +14,8 @@ namespace Longman\TelegramBot\Commands\UserCommands;
use
Longman\TelegramBot\Commands\UserCommand
;
use
Longman\TelegramBot\Entities\File
;
use
Longman\TelegramBot\Entities\PhotoSize
;
use
Longman\TelegramBot\Entities\UserProfilePhotos
;
use
Longman\TelegramBot\Request
;
/**
...
...
@@ -39,60 +41,63 @@ class WhoamiCommand extends UserCommand
/**
* @var string
*/
protected
$version
=
'1.0.1'
;
/**
* @var bool
*/
protected
$public
=
true
;
protected
$version
=
'1.1.0'
;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$user_id
=
$message
->
getFrom
()
->
getId
();
$from
=
$message
->
getFrom
();
$user_id
=
$from
->
getId
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$message_id
=
$message
->
getMessageId
();
$text
=
$message
->
getText
(
true
);
$data
=
[
'chat_id'
=>
$chat_id
,
'reply_to_message_id'
=>
$message_id
,
];
//Send chat action
Request
::
sendChatAction
([
'chat_id'
=>
$chat_id
,
'action'
=>
'typing'
]);
Request
::
sendChatAction
([
'chat_id'
=>
$chat_id
,
'action'
=>
'typing'
,
]);
$caption
=
'Your Id: '
.
$user_id
.
"
\n
"
;
$caption
.=
'Name: '
.
$message
->
getFrom
()
->
getFirstName
()
.
' '
.
$message
->
getFrom
()
->
getLastName
()
.
"
\n
"
;
$caption
.=
'Username: '
.
$message
->
getFrom
()
->
getUsername
();
$caption
=
sprintf
(
'Your Id: %d'
.
PHP_EOL
.
'Name: %s %s'
.
PHP_EOL
.
'Username: %s'
,
$user_id
,
$from
->
getFirstName
(),
$from
->
getLastName
(),
$from
->
getUsername
()
);
//Fetch user profile photo
$limit
=
10
;
$offset
=
null
;
$ServerResponse
=
Request
::
getUserProfilePhotos
([
$response
=
Request
::
getUserProfilePhotos
(
[
'user_id'
=>
$user_id
,
'limit'
=>
$limit
,
'offset'
=>
$offset
,
]);
]
);
//Check if the request isOK
if
(
$ServerResponse
->
isOk
())
{
$UserProfilePhoto
=
$ServerResponse
->
getResult
();
$totalcount
=
$UserProfilePhoto
->
getTotalCount
();
}
else
{
$totalcount
=
0
;
}
if
(
$response
->
isOk
())
{
/** @var UserProfilePhotos $user_profile_photos */
$user_profile_photos
=
$response
->
getResult
();
$data
=
[
'chat_id'
=>
$chat_id
,
'reply_to_message_id'
=>
$message_id
,
];
if
(
$user_profile_photos
->
getTotalCount
()
>
0
)
{
$photos
=
$user_profile_photos
->
getPhotos
();
if
(
$totalcount
>
0
)
{
$photos
=
$UserProfilePhoto
->
getPhotos
();
//I pick the latest photo with the hight definition
/** @var PhotoSize $photo */
$photo
=
$photos
[
0
][
2
];
$file_id
=
$photo
->
getFileId
();
...
...
@@ -101,19 +106,21 @@ class WhoamiCommand extends UserCommand
$result
=
Request
::
sendPhoto
(
$data
);
//Download the image pictures
//Download after send message response to speedup response
$file_id
=
$photo
->
getFileId
();
$ServerResponse
=
Request
::
getFile
([
'file_id'
=>
$file_id
]);
if
(
$ServerResponse
->
isOk
())
{
Request
::
downloadFile
(
$ServerResponse
->
getResult
());
//Download the photo after send message response to speedup response
$response2
=
Request
::
getFile
([
'file_id'
=>
$file_id
]);
if
(
$response2
->
isOk
())
{
/** @var File $photo_file */
$photo_file
=
$response2
->
getResult
();
Request
::
downloadFile
(
$photo_file
);
}
return
$result
;
}
}
}
else
{
//No Photo just send text
$data
[
'text'
]
=
$caption
;
$result
=
Request
::
sendMessage
(
$data
);
}
return
$result
;
return
Request
::
sendMessage
(
$data
)
;
}
}
src/DB.php
View file @
818c3c84
...
...
@@ -16,6 +16,7 @@ use Longman\TelegramBot\Entities\Chat;
use
Longman\TelegramBot\Entities\ChosenInlineResult
;
use
Longman\TelegramBot\Entities\InlineQuery
;
use
Longman\TelegramBot\Entities\Message
;
use
Longman\TelegramBot\Entities\ReplyToMessage
;
use
Longman\TelegramBot\Entities\Update
;
use
Longman\TelegramBot\Entities\User
;
use
Longman\TelegramBot\Exception\TelegramException
;
...
...
@@ -87,7 +88,7 @@ class DB
self
::
$mysql_credentials
=
$credentials
;
self
::
$table_prefix
=
$table_prefix
;
self
::
defineTable
();
self
::
defineTable
s
();
return
self
::
$pdo
;
}
...
...
@@ -104,9 +105,12 @@ class DB
* @return PDO PDO database object
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
externalInitialize
(
$external_pdo_connection
,
Telegram
$telegram
,
$table_prefix
=
null
)
{
if
(
empty
(
$external_pdo_connection
))
{
public
static
function
externalInitialize
(
$external_pdo_connection
,
Telegram
$telegram
,
$table_prefix
=
null
)
{
if
(
$external_pdo_connection
===
null
)
{
throw
new
TelegramException
(
'MySQL external connection not provided!'
);
}
...
...
@@ -115,42 +119,32 @@ class DB
self
::
$mysql_credentials
=
null
;
self
::
$table_prefix
=
$table_prefix
;
self
::
defineTable
();
self
::
defineTable
s
();
return
self
::
$pdo
;
}
/**
* Define all the table with the proper prefix
* Define all the table
s
with the proper prefix
*/
protected
static
function
defineTable
()
protected
static
function
defineTable
s
()
{
if
(
!
defined
(
'TB_TELEGRAM_UPDATE'
))
{
define
(
'TB_TELEGRAM_UPDATE'
,
self
::
$table_prefix
.
'telegram_update'
);
}
if
(
!
defined
(
'TB_MESSAGE'
))
{
define
(
'TB_MESSAGE'
,
self
::
$table_prefix
.
'message'
);
}
if
(
!
defined
(
'TB_EDITED_MESSAGE'
))
{
define
(
'TB_EDITED_MESSAGE'
,
self
::
$table_prefix
.
'edited_message'
);
}
if
(
!
defined
(
'TB_INLINE_QUERY'
))
{
define
(
'TB_INLINE_QUERY'
,
self
::
$table_prefix
.
'inline_query'
);
}
if
(
!
defined
(
'TB_CHOSEN_INLINE_RESULT'
))
{
define
(
'TB_CHOSEN_INLINE_RESULT'
,
self
::
$table_prefix
.
'chosen_inline_result'
);
}
if
(
!
defined
(
'TB_CALLBACK_QUERY'
))
{
define
(
'TB_CALLBACK_QUERY'
,
self
::
$table_prefix
.
'callback_query'
);
$tables
=
[
'callback_query'
,
'chat'
,
'chosen_inline_result'
,
'edited_message'
,
'inline_query'
,
'message'
,
'telegram_update'
,
'user'
,
'user_chat'
,
];
foreach
(
$tables
as
$table
)
{
$table_name
=
'TB_'
.
strtoupper
(
$table
);
if
(
!
defined
(
$table_name
))
{
define
(
$table_name
,
self
::
$table_prefix
.
$table
);
}
if
(
!
defined
(
'TB_USER'
))
{
define
(
'TB_USER'
,
self
::
$table_prefix
.
'user'
);
}
if
(
!
defined
(
'TB_CHAT'
))
{
define
(
'TB_CHAT'
,
self
::
$table_prefix
.
'chat'
);
}
if
(
!
defined
(
'TB_USER_CHAT'
))
{
define
(
'TB_USER_CHAT'
,
self
::
$table_prefix
.
'user_chat'
);
}
}
...
...
@@ -161,11 +155,7 @@ class DB
*/
public
static
function
isDbConnected
()
{
if
(
empty
(
self
::
$pdo
))
{
return
false
;
}
else
{
return
true
;
}
return
self
::
$pdo
!==
null
;
}
/**
...
...
@@ -183,22 +173,24 @@ class DB
}
try
{
$query
=
'SELECT `id` FROM `'
.
TB_TELEGRAM_UPDATE
.
'` '
;
$query
.=
'ORDER BY `id` DESC'
;
$sql
=
'
SELECT `id`
FROM `'
.
TB_TELEGRAM_UPDATE
.
'`
ORDER BY `id` DESC
'
;
if
(
!
is_null
(
$limit
)
)
{
$
query
.=
' LIMIT :limit
'
;
if
(
$limit
!==
null
)
{
$
sql
.=
'LIMIT :limit
'
;
}
$sth_select_telegram_update
=
self
::
$pdo
->
prepare
(
$query
);
$sth_select_telegram_update
->
bindParam
(
':limit'
,
$limit
,
PDO
::
PARAM_INT
);
$sth_select_telegram_update
->
execute
();
$results
=
$sth_select_telegram_update
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
$sth
=
self
::
$pdo
->
prepare
(
$sql
);
$sth
->
bindParam
(
':limit'
,
$limit
,
PDO
::
PARAM_INT
);
$sth
->
execute
();
return
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$results
;
}
/**
...
...
@@ -216,41 +208,67 @@ class DB
}
try
{
//message table
$query
=
'SELECT * FROM `'
.
TB_MESSAGE
.
'` '
;
$query
.=
'WHERE '
.
TB_MESSAGE
.
'.`update_id` != 0 '
;
$query
.=
'ORDER BY '
.
TB_MESSAGE
.
'.`message_id` DESC'
;
$sql
=
'
SELECT *
FROM `'
.
TB_MESSAGE
.
'`
WHERE `update_id` != 0
ORDER BY `message_id` DESC
'
;
if
(
!
is_null
(
$limit
)
)
{
$
query
.=
' LIMIT :limit
'
;
if
(
$limit
!==
null
)
{
$
sql
.=
'LIMIT :limit
'
;
}
$sth
=
self
::
$pdo
->
prepare
(
$
query
);
$sth
=
self
::
$pdo
->
prepare
(
$
sql
);
$sth
->
bindParam
(
':limit'
,
$limit
,
PDO
::
PARAM_INT
);
$sth
->
execute
();
$results
=
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
return
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$results
;
}
/**
* Convert from unix timestamp to timestamp
*
* @param int $time Unix timestamp
* @param int $time Unix timestamp
(if null, current timestamp is used)
*
* @return
null|string Timestamp if a time has been passed, else null
* @return
string
*/
protected
static
function
getTimestamp
(
$time
=
null
)
{
if
(
is_null
(
$time
)
)
{
return
date
(
'Y-m-d H:i:s'
,
time
()
);
if
(
$time
===
null
)
{
$time
=
time
(
);
}
return
date
(
'Y-m-d H:i:s'
,
$time
);
}
/**
* Convert array of Entity items to a JSON array
*
* @todo Find a better way, as json_* functions are very heavy
*
* @param array|null $entities
* @param mixed $default
*
* @return mixed
*/
public
static
function
entitiesArrayToJson
(
$entities
,
$default
=
null
)
{
if
(
!
is_array
(
$entities
))
{
return
$default
;
}
//Convert each Entity item into an object based on its JSON reflection
$json_entities
=
array_map
(
function
(
$entity
)
{
return
json_decode
(
$entity
,
true
);
},
$entities
);
return
json_encode
(
$json_entities
);
}
/**
* Insert entry to telegram_update table
*
...
...
@@ -274,7 +292,7 @@ class DB
$callback_query_id
,
$edited_message_id
)
{
if
(
is_null
(
$message_id
)
&&
is_null
(
$inline_query_id
)
&&
is_null
(
$chosen_inline_result_id
)
&&
is_null
(
$callback_query_id
)
&&
is_null
(
$edited_message_id
)
)
{
if
(
$message_id
===
null
&&
$inline_query_id
===
null
&&
$chosen_inline_result_id
===
null
&&
$callback_query_id
===
null
&&
$edited_message_id
===
null
)
{
throw
new
TelegramException
(
'message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null'
);
}
...
...
@@ -283,28 +301,22 @@ class DB
}
try
{
$sth_insert_telegram_update
=
self
::
$pdo
->
prepare
(
'INSERT IGNORE INTO `'
.
TB_TELEGRAM_UPDATE
.
'`
(
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`
)
VALUES (
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id
)
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT IGNORE INTO `'
.
TB_TELEGRAM_UPDATE
.
'`
(`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`)
VALUES
(:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id)
'
);
$sth_insert_telegram_update
->
bindParam
(
':id'
,
$id
,
PDO
::
PARAM_INT
);
$sth_insert_telegram_update
->
bindParam
(
':chat_id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth_insert_telegram_update
->
bindParam
(
':message_id'
,
$message_id
,
PDO
::
PARAM_INT
);
$sth_insert_telegram_update
->
bindParam
(
':inline_query_id'
,
$inline_query_id
,
PDO
::
PARAM_INT
);
$sth_insert_telegram_update
->
bindParam
(
':chosen_inline_result_id'
,
$chosen_inline_result_id
,
PDO
::
PARAM_INT
);
$sth_insert_telegram_update
->
bindParam
(
':callback_query_id'
,
$callback_query_id
,
PDO
::
PARAM_INT
);
$sth_insert_telegram_update
->
bindParam
(
':edited_message_id'
,
$edited_message_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':id'
,
$id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':chat_id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':message_id'
,
$message_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':inline_query_id'
,
$inline_query_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':chosen_inline_result_id'
,
$chosen_inline_result_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':callback_query_id'
,
$callback_query_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':edited_message_id'
,
$edited_message_id
,
PDO
::
PARAM_INT
);
return
$sth
_insert_telegram_update
->
execute
();
return
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -332,44 +344,44 @@ class DB
$last_name
=
$user
->
getLastName
();
try
{
$sth1
=
self
::
$pdo
->
prepare
(
'INSERT INTO `'
.
TB_USER
.
'`
(
`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`
)
VALUES (
:id, :username, :first_name, :last_name, :date, :date
)
ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name,
`last_name`=:last_name, `updated_at`=:date
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT INTO `'
.
TB_USER
.
'`
(`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`)
VALUES
(:id, :username, :first_name, :last_name, :date, :date)
ON DUPLICATE KEY UPDATE
`username` = :username,
`first_name` = :first_name,
`last_name` = :last_name,
`updated_at` = :date
'
);
$sth
1
->
bindParam
(
':id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
1
->
bindParam
(
':username'
,
$username
,
PDO
::
PARAM_STR
,
255
);
$sth
1
->
bindParam
(
':first_name'
,
$first_name
,
PDO
::
PARAM_STR
,
255
);
$sth
1
->
bindParam
(
':last_name'
,
$last_name
,
PDO
::
PARAM_STR
,
255
);
$sth
1
->
bindParam
(
':date'
,
$date
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':username'
,
$username
,
PDO
::
PARAM_STR
,
255
);
$sth
->
bindParam
(
':first_name'
,
$first_name
,
PDO
::
PARAM_STR
,
255
);
$sth
->
bindParam
(
':last_name'
,
$last_name
,
PDO
::
PARAM_STR
,
255
);
$sth
->
bindParam
(
':date'
,
$date
,
PDO
::
PARAM_STR
);
$status
=
$sth
1
->
execute
();
$status
=
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
//insert also the relationship to the chat into user_chat table
if
(
!
is_null
(
$chat
)
)
{
if
(
$chat
instanceof
Chat
)
{
$chat_id
=
$chat
->
getId
();
try
{
$sth3
=
self
::
$pdo
->
prepare
(
'INSERT IGNORE INTO `'
.
TB_USER_CHAT
.
'`
(
`user_id`, `chat_id`
)
VALUES (
:user_id, :chat_id
)'
);
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT IGNORE INTO `'
.
TB_USER_CHAT
.
'`
(`user_id`, `chat_id`)
VALUES
(:user_id, :chat_id)
'
);
$sth
3
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
3
->
bindParam
(
':chat_id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':chat_id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$status
=
$sth
3
->
execute
();
$status
=
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -396,34 +408,35 @@ class DB
$chat_id
=
$chat
->
getId
();
$chat_title
=
$chat
->
getTitle
();
$
type
=
$chat
->
getType
();
$
chat_type
=
$chat
->
getType
();
try
{
$sth2
=
self
::
$pdo
->
prepare
(
'INSERT INTO `'
.
TB_CHAT
.
'`
(
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
)
VALUES (
:id, :type, :title, :date, :date, :oldid
)
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT INTO `'
.
TB_CHAT
.
'`
(`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`)
VALUES
(:id, :type, :title, :date, :date, :oldid)
ON DUPLICATE KEY UPDATE
`type` = :type,
`title` = :title,
`updated_at` = :date
'
);
if
(
$migrate_to_chat_id
)
{
$type
=
'supergroup'
;
$
chat_
type
=
'supergroup'
;
$sth
2
->
bindParam
(
':id'
,
$migrate_to_chat_id
,
PDO
::
PARAM_INT
);
$sth
2
->
bindParam
(
':oldid'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':id'
,
$migrate_to_chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':oldid'
,
$chat_id
,
PDO
::
PARAM_INT
);
}
else
{
$sth
2
->
bindParam
(
':id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
2
->
bindParam
(
':oldid'
,
$migrate_to_chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':oldid'
,
$migrate_to_chat_id
,
PDO
::
PARAM_INT
);
}
$sth
2
->
bindParam
(
':type'
,
$
type
,
PDO
::
PARAM_INT
);
$sth
2
->
bindParam
(
':title'
,
$chat_title
,
PDO
::
PARAM_STR
,
255
);
$sth
2
->
bindParam
(
':date'
,
$date
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':type'
,
$chat_
type
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':title'
,
$chat_title
,
PDO
::
PARAM_STR
,
255
);
$sth
->
bindParam
(
':date'
,
$date
,
PDO
::
PARAM_STR
);
return
$sth
2
->
execute
();
return
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -437,30 +450,36 @@ class DB
* @param \Longman\TelegramBot\Entities\Update $update
*
* @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
insertRequest
(
Update
$update
)
{
$update_id
=
$update
->
getUpdateId
();
if
(
$update
->
getUpdateType
()
==
'message'
)
{
$update_type
=
$update
->
getUpdateType
();
if
(
$update_type
===
'message'
)
{
$message
=
$update
->
getMessage
();
if
(
self
::
insertMessageRequest
(
$message
))
{
$message_id
=
$message
->
getMessageId
();
$chat_id
=
$message
->
getChat
()
->
getId
();
return
self
::
insertTelegramUpdate
(
$update_id
,
$chat_id
,
$message_id
,
null
,
null
,
null
,
null
);
}
}
elseif
(
$update
->
getUpdateType
()
==
'inline_query'
)
{
}
elseif
(
$update
_type
=
==
'inline_query'
)
{
$inline_query
=
$update
->
getInlineQuery
();
if
(
self
::
insertInlineQueryRequest
(
$inline_query
))
{
$inline_query_id
=
$inline_query
->
getId
();
return
self
::
insertTelegramUpdate
(
$update_id
,
null
,
null
,
$inline_query_id
,
null
,
null
,
null
);
}
}
elseif
(
$update
->
getUpdateType
()
==
'chosen_inline_result'
)
{
}
elseif
(
$update
_type
=
==
'chosen_inline_result'
)
{
$chosen_inline_result
=
$update
->
getChosenInlineResult
();
if
(
self
::
insertChosenInlineResultRequest
(
$chosen_inline_result
))
{
$chosen_inline_result_local_id
=
self
::
$pdo
->
lastInsertId
();
return
self
::
insertTelegramUpdate
(
$update_id
,
null
,
...
...
@@ -471,19 +490,21 @@ class DB
null
);
}
}
elseif
(
$update
->
getUpdateType
()
==
'callback_query'
)
{
}
elseif
(
$update
_type
=
==
'callback_query'
)
{
$callback_query
=
$update
->
getCallbackQuery
();
if
(
self
::
insertCallbackQueryRequest
(
$callback_query
))
{
$callback_query_id
=
$callback_query
->
getId
();
return
self
::
insertTelegramUpdate
(
$update_id
,
null
,
null
,
null
,
null
,
$callback_query_id
,
null
);
}
}
elseif
(
$update
->
getUpdateType
()
==
'edited_message'
)
{
}
elseif
(
$update
_type
=
==
'edited_message'
)
{
$edited_message
=
$update
->
getEditedMessage
();
if
(
self
::
insertEditedMessageRequest
(
$edited_message
))
{
$chat_id
=
$edited_message
->
getChat
()
->
getId
();
$edited_message_local_id
=
self
::
$pdo
->
lastInsertId
();
return
self
::
insertTelegramUpdate
(
$update_id
,
$chat_id
,
...
...
@@ -514,21 +535,18 @@ class DB
}
try
{
$mysql_query
=
'INSERT IGNORE INTO `'
.
TB_INLINE_QUERY
.
'`
(
`id`, `user_id`, `location`, `query`, `offset`, `created_at`
)
VALUES (
:inline_query_id, :user_id, :location, :query, :param_offset, :created_at
)'
;
$sth_insert_inline_query
=
self
::
$pdo
->
prepare
(
$mysql_query
);
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT IGNORE INTO `'
.
TB_INLINE_QUERY
.
'`
(`id`, `user_id`, `location`, `query`, `offset`, `created_at`)
VALUES
(:inline_query_id, :user_id, :location, :query, :param_offset, :created_at)
'
);
$date
=
self
::
getTimestamp
(
time
()
);
$date
=
self
::
getTimestamp
();
$inline_query_id
=
$inline_query
->
getId
();
$from
=
$inline_query
->
getFrom
();
$user_id
=
null
;
if
(
is_object
(
$from
)
)
{
if
(
$from
instanceof
User
)
{
$user_id
=
$from
->
getId
();
self
::
insertUser
(
$from
,
$date
);
}
...
...
@@ -537,14 +555,14 @@ class DB
$query
=
$inline_query
->
getQuery
();
$offset
=
$inline_query
->
getOffset
();
$sth
_insert_inline_query
->
bindParam
(
':inline_query_id'
,
$inline_query_id
,
PDO
::
PARAM_INT
);
$sth
_insert_inline_query
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
_insert_inline_query
->
bindParam
(
':location'
,
$location
,
PDO
::
PARAM_STR
);
$sth
_insert_inline_query
->
bindParam
(
':query'
,
$query
,
PDO
::
PARAM_STR
);
$sth
_insert_inline_query
->
bindParam
(
':param_offset'
,
$offset
,
PDO
::
PARAM_STR
);
$sth
_insert_inline_query
->
bindParam
(
':created_at'
,
$date
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':inline_query_id'
,
$inline_query_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':location'
,
$location
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':query'
,
$query
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':param_offset'
,
$offset
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':created_at'
,
$date
,
PDO
::
PARAM_STR
);
return
$sth
_insert_inline_query
->
execute
();
return
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -565,21 +583,18 @@ class DB
}
try
{
$mysql_query
=
'INSERT INTO `'
.
TB_CHOSEN_INLINE_RESULT
.
'`
(
`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at`
)
VALUES (
:result_id, :user_id, :location, :inline_message_id, :query, :created_at
)'
;
$sth_insert_chosen_inline_result
=
self
::
$pdo
->
prepare
(
$mysql_query
);
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT INTO `'
.
TB_CHOSEN_INLINE_RESULT
.
'`
(`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at`)
VALUES
(:result_id, :user_id, :location, :inline_message_id, :query, :created_at)
'
);
$date
=
self
::
getTimestamp
(
time
()
);
$date
=
self
::
getTimestamp
();
$result_id
=
$chosen_inline_result
->
getResultId
();
$from
=
$chosen_inline_result
->
getFrom
();
$user_id
=
null
;
if
(
is_object
(
$from
)
)
{
if
(
$from
instanceof
User
)
{
$user_id
=
$from
->
getId
();
self
::
insertUser
(
$from
,
$date
);
}
...
...
@@ -588,14 +603,14 @@ class DB
$inline_message_id
=
$chosen_inline_result
->
getInlineMessageId
();
$query
=
$chosen_inline_result
->
getQuery
();
$sth
_insert_chosen_inline_result
->
bindParam
(
':result_id'
,
$result_id
,
PDO
::
PARAM_STR
);
$sth
_insert_chosen_inline_result
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
_insert_chosen_inline_result
->
bindParam
(
':location'
,
$location
,
PDO
::
PARAM_INT
);
$sth
_insert_chosen_inline_result
->
bindParam
(
':inline_message_id'
,
$inline_message_id
,
PDO
::
PARAM_STR
);
$sth
_insert_chosen_inline_result
->
bindParam
(
':query'
,
$query
,
PDO
::
PARAM_STR
);
$sth
_insert_chosen_inline_result
->
bindParam
(
':created_at'
,
$date
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':result_id'
,
$result_id
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':location'
,
$location
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':inline_message_id'
,
$inline_message_id
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':query'
,
$query
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':created_at'
,
$date
,
PDO
::
PARAM_STR
);
return
$sth
_insert_chosen_inline_result
->
execute
();
return
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -616,22 +631,18 @@ class DB
}
try
{
$mysql_query
=
'INSERT IGNORE INTO `'
.
TB_CALLBACK_QUERY
.
'`
(
`id`, `user_id`, `chat_id`, `message_id`, `inline_message_id`, `data`, `created_at`
)
VALUES (
:callback_query_id, :user_id, :chat_id, :message_id, :inline_message_id, :data, :created_at
)'
;
$sth_insert_callback_query
=
self
::
$pdo
->
prepare
(
$mysql_query
);
$date
=
self
::
getTimestamp
(
time
());
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT IGNORE INTO `'
.
TB_CALLBACK_QUERY
.
'`
(`id`, `user_id`, `chat_id`, `message_id`, `inline_message_id`, `data`, `created_at`)
VALUES
(:callback_query_id, :user_id, :chat_id, :message_id, :inline_message_id, :data, :created_at)
'
);
$date
=
self
::
getTimestamp
();
$callback_query_id
=
$callback_query
->
getId
();
$from
=
$callback_query
->
getFrom
();
$user_id
=
null
;
if
(
is_object
(
$from
)
)
{
if
(
$from
instanceof
User
)
{
$user_id
=
$from
->
getId
();
self
::
insertUser
(
$from
,
$date
);
}
...
...
@@ -639,15 +650,19 @@ class DB
$message
=
$callback_query
->
getMessage
();
$chat_id
=
null
;
$message_id
=
null
;
if
(
$message
)
{
if
(
$message
instanceof
Message
)
{
$chat_id
=
$message
->
getChat
()
->
getId
();
$message_id
=
$message
->
getMessageId
();
$sth
=
self
::
$pdo
->
prepare
(
'SELECT * FROM `'
.
TB_MESSAGE
.
'`
WHERE `id` = '
.
$message_id
.
' AND `chat_id` = '
.
$chat_id
.
' LIMIT 1'
);
$sth
->
execute
();
$is_message
=
self
::
$pdo
->
query
(
'
SELECT *
FROM `'
.
TB_MESSAGE
.
'`
WHERE `id` = '
.
$message_id
.
'
AND `chat_id` = '
.
$chat_id
.
'
LIMIT 1
'
)
->
rowCount
();
if
(
$
sth
->
rowCount
()
>
0
)
{
if
(
$
is_message
)
{
self
::
insertEditedMessageRequest
(
$message
);
}
else
{
self
::
insertMessageRequest
(
$message
);
...
...
@@ -657,15 +672,15 @@ class DB
$inline_message_id
=
$callback_query
->
getInlineMessageId
();
$data
=
$callback_query
->
getData
();
$sth
_insert_callback_query
->
bindParam
(
':callback_query_id'
,
$callback_query_id
,
PDO
::
PARAM_INT
);
$sth
_insert_callback_query
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
_insert_callback_query
->
bindParam
(
':chat_id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
_insert_callback_query
->
bindParam
(
':message_id'
,
$message_id
,
PDO
::
PARAM_INT
);
$sth
_insert_callback_query
->
bindParam
(
':inline_message_id'
,
$inline_message_id
,
PDO
::
PARAM_STR
);
$sth
_insert_callback_query
->
bindParam
(
':data'
,
$data
,
PDO
::
PARAM_STR
);
$sth
_insert_callback_query
->
bindParam
(
':created_at'
,
$date
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':callback_query_id'
,
$callback_query_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':chat_id'
,
$chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':message_id'
,
$message_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':inline_message_id'
,
$inline_message_id
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':data'
,
$data
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':created_at'
,
$date
,
PDO
::
PARAM_STR
);
return
$sth
_insert_callback_query
->
execute
();
return
$sth
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -694,10 +709,10 @@ class DB
$forward_from
=
$message
->
getForwardFrom
();
$forward_from_chat
=
$message
->
getForwardFromChat
();
$photo
=
$message
->
getPhoto
(
);
$entities
=
$message
->
getEntities
(
);
$photo
=
self
::
entitiesArrayToJson
(
$message
->
getPhoto
(),
''
);
$entities
=
self
::
entitiesArrayToJson
(
$message
->
getEntities
(),
null
);
$new_chat_member
=
$message
->
getNewChatMember
();
$new_chat_photo
=
$message
->
getNewChatPhoto
(
);
$new_chat_photo
=
self
::
entitiesArrayToJson
(
$message
->
getNewChatPhoto
(),
''
);
$left_chat_member
=
$message
->
getLeftChatMember
();
$migrate_to_chat_id
=
$message
->
getMigrateToChatId
();
...
...
@@ -708,31 +723,32 @@ class DB
self
::
insertUser
(
$from
,
$date
,
$chat
);
//Insert the forwarded message user in users table
if
(
is_object
(
$forward_from
)
)
{
if
(
$forward_from
instanceof
User
)
{
$forward_date
=
self
::
getTimestamp
(
$message
->
getForwardDate
());
self
::
insertUser
(
$forward_from
,
$forward_date
);
$forward_from
=
$forward_from
->
getId
();
}
if
(
is_object
(
$forward_from_chat
)
)
{
if
(
$forward_from_chat
instanceof
Chat
)
{
$forward_date
=
self
::
getTimestamp
(
$message
->
getForwardDate
());
self
::
insertChat
(
$forward_from_chat
,
$forward_date
);
$forward_from_chat
=
$forward_from_chat
->
getId
();
}
//New and left chat member
if
(
$new_chat_member
)
{
if
(
$new_chat_member
instanceof
User
)
{
//Insert the new chat user
self
::
insertUser
(
$new_chat_member
,
$date
,
$chat
);
$new_chat_member
=
$new_chat_member
->
getId
();
}
elseif
(
$left_chat_member
)
{
}
elseif
(
$left_chat_member
instanceof
User
)
{
//Insert the left chat user
self
::
insertUser
(
$left_chat_member
,
$date
,
$chat
);
$left_chat_member
=
$left_chat_member
->
getId
();
}
try
{
$sth
=
self
::
$pdo
->
prepare
(
'INSERT IGNORE INTO `'
.
TB_MESSAGE
.
'`
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT IGNORE INTO `'
.
TB_MESSAGE
.
'`
(
`id`, `user_id`, `chat_id`, `date`, `forward_from`, `forward_from_chat`,
`forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
...
...
@@ -741,8 +757,7 @@ class DB
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
`supergroup_chat_created`, `channel_chat_created`,
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
)
VALUES (
) VALUES (
:message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat,
:forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
:photo, :sticker, :video, :voice, :caption, :contact,
...
...
@@ -758,7 +773,7 @@ class DB
$reply_to_message
=
$message
->
getReplyToMessage
();
$reply_to_message_id
=
null
;
if
(
is_object
(
$reply_to_message
)
)
{
if
(
$reply_to_message
instanceof
ReplyToMessage
)
{
$reply_to_message_id
=
$reply_to_message
->
getMessageId
();
// please notice that, as explained in the documentation, reply_to_message don't contain other
// reply_to_message field so recursion deep is 1
...
...
@@ -792,40 +807,17 @@ class DB
$sth
->
bindParam
(
':forward_from_chat'
,
$forward_from_chat
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':forward_date'
,
$forward_date
,
PDO
::
PARAM_STR
);
$reply_chat_id
=
null
;
$reply_
to_
chat_id
=
null
;
if
(
$reply_to_message_id
)
{
$reply_chat_id
=
$chat_id
;
}
$var
=
[];
if
(
is_array
(
$entities
))
{
foreach
(
$entities
as
$elm
)
{
$var
[]
=
json_decode
(
$elm
,
true
);
}
$entities
=
json_encode
(
$var
);
}
else
{
$entities
=
null
;
$reply_to_chat_id
=
$chat_id
;
}
$sth
->
bindParam
(
':reply_to_chat'
,
$reply_chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':reply_to_chat'
,
$reply_
to_
chat_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':reply_to_message'
,
$reply_to_message_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':text'
,
$text
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':entities'
,
$entities
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':audio'
,
$audio
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':document'
,
$document
,
PDO
::
PARAM_STR
);
$var
=
[];
if
(
is_array
(
$photo
))
{
foreach
(
$photo
as
$elm
)
{
$var
[]
=
json_decode
(
$elm
,
true
);
}
$photo
=
json_encode
(
$var
);
}
else
{
$photo
=
''
;
}
$sth
->
bindParam
(
':photo'
,
$photo
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':sticker'
,
$sticker
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':video'
,
$video
,
PDO
::
PARAM_STR
);
...
...
@@ -837,19 +829,6 @@ class DB
$sth
->
bindParam
(
':new_chat_member'
,
$new_chat_member
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':left_chat_member'
,
$left_chat_member
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':new_chat_title'
,
$new_chat_title
,
PDO
::
PARAM_STR
);
//Array of PhotoSize
$var
=
[];
if
(
is_array
(
$new_chat_photo
))
{
foreach
(
$new_chat_photo
as
$elm
)
{
$var
[]
=
json_decode
(
$elm
,
true
);
}
$new_chat_photo
=
json_encode
(
$var
);
}
else
{
$new_chat_photo
=
''
;
}
$sth
->
bindParam
(
':new_chat_photo'
,
$new_chat_photo
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':delete_chat_photo'
,
$delete_chat_photo
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':group_chat_created'
,
$group_chat_created
,
PDO
::
PARAM_STR
);
...
...
@@ -886,7 +865,7 @@ class DB
$edit_date
=
self
::
getTimestamp
(
$edited_message
->
getEditDate
());
$entities
=
$edited_message
->
getEntities
(
);
$entities
=
self
::
entitiesArrayToJson
(
$edited_message
->
getEntities
(),
null
);
//Insert chat
self
::
insertChat
(
$chat
,
$edit_date
);
...
...
@@ -895,13 +874,12 @@ class DB
self
::
insertUser
(
$from
,
$edit_date
,
$chat
);
try
{
$sth
=
self
::
$pdo
->
prepare
(
'INSERT INTO `'
.
TB_EDITED_MESSAGE
.
'`
(
`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`
)
VALUES (
:chat_id, :message_id, :user_id, :date, :text, :entities, :caption
)'
);
$sth
=
self
::
$pdo
->
prepare
(
'
INSERT IGNORE INTO `'
.
TB_EDITED_MESSAGE
.
'`
(`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`)
VALUES
(:chat_id, :message_id, :user_id, :date, :text, :entities, :caption)
'
);
$message_id
=
$edited_message
->
getMessageId
();
$from_id
=
$from
->
getId
();
...
...
@@ -913,18 +891,6 @@ class DB
$sth
->
bindParam
(
':message_id'
,
$message_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':user_id'
,
$from_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':date'
,
$edit_date
,
PDO
::
PARAM_STR
);
$var
=
[];
if
(
is_array
(
$entities
))
{
foreach
(
$entities
as
$elm
)
{
$var
[]
=
json_decode
(
$elm
,
true
);
}
$entities
=
json_encode
(
$var
);
}
else
{
$entities
=
null
;
}
$sth
->
bindParam
(
':text'
,
$text
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':entities'
,
$entities
,
PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':caption'
,
$caption
,
PDO
::
PARAM_STR
);
...
...
@@ -967,77 +933,68 @@ class DB
}
try
{
$query
=
'SELECT * ,
$query
=
'
SELECT * ,
'
.
TB_CHAT
.
'.`id` AS `chat_id`,
'
.
TB_CHAT
.
'.`created_at` AS `chat_created_at`,
'
.
TB_CHAT
.
'.`updated_at` AS `chat_updated_at`
'
.
((
$select_users
)
?
', '
.
TB_USER
.
'.`id` AS `user_id` FROM `'
.
TB_CHAT
.
'` LEFT JOIN `'
.
TB_USER
.
'`
ON '
.
TB_CHAT
.
'.`id`='
.
TB_USER
.
'.`id`'
:
'FROM `'
.
TB_CHAT
.
'`'
);
'
;
if
(
$select_users
)
{
$query
.=
'
, '
.
TB_USER
.
'.`id` AS `user_id`
FROM `'
.
TB_CHAT
.
'`
LEFT JOIN `'
.
TB_USER
.
'`
ON '
.
TB_CHAT
.
'.`id`='
.
TB_USER
.
'.`id`
'
;
}
else
{
$query
.=
'FROM `'
.
TB_CHAT
.
'`'
;
}
//Building parts of query
$where
=
[];
$tokens
=
[];
if
(
!
$select_groups
||
!
$select_users
||
!
$select_super_groups
)
{
$chat_or_user
=
''
;
if
(
$select_groups
)
{
$chat_or_user
.=
TB_CHAT
.
'.`type` = "group"'
;
}
if
(
$select_super_groups
)
{
if
(
!
empty
(
$chat_or_user
))
{
$chat_or_user
.=
' OR '
;
}
$chat_or_user
.=
TB_CHAT
.
'.`type` = "supergroup"'
;
}
if
(
$select_users
)
{
if
(
!
empty
(
$chat_or_user
))
{
$chat_or_user
.=
' OR '
;
}
$chat_or_user
=
[];
$chat_or_user
.=
TB_CHAT
.
'.`type` = "private"'
;
}
$select_groups
&&
$chat_or_user
[]
=
TB_CHAT
.
'.`type` = "group"'
;
$select_super_groups
&&
$chat_or_user
[]
=
TB_CHAT
.
'.`type` = "supergroup"'
;
$select_users
&&
$chat_or_user
[]
=
TB_CHAT
.
'.`type` = "private"'
;
$where
[]
=
'('
.
$chat_or_user
.
')'
;
$where
[]
=
'('
.
implode
(
' OR '
,
$chat_or_user
)
.
')'
;
}
if
(
!
is_null
(
$date_from
)
)
{
if
(
null
!==
$date_from
)
{
$where
[]
=
TB_CHAT
.
'.`updated_at` >= :date_from'
;
$tokens
[
':date_from'
]
=
$date_from
;
}
if
(
!
is_null
(
$date_to
)
)
{
if
(
null
!==
$date_to
)
{
$where
[]
=
TB_CHAT
.
'.`updated_at` <= :date_to'
;
$tokens
[
':date_to'
]
=
$date_to
;
}
if
(
!
is_null
(
$chat_id
)
)
{
if
(
null
!==
$chat_id
)
{
$where
[]
=
TB_CHAT
.
'.`id` = :chat_id'
;
$tokens
[
':chat_id'
]
=
$chat_id
;
}
if
(
!
is_null
(
$text
)
)
{
if
(
null
!==
$text
)
{
if
(
$select_users
)
{
$where
[]
=
'(LOWER('
.
TB_CHAT
.
'.`title`) LIKE :text OR LOWER('
.
TB_USER
.
'.`first_name`) LIKE :text OR LOWER('
.
TB_USER
.
'.`last_name`) LIKE :text OR LOWER('
.
TB_USER
.
'.`username`) LIKE :text)'
;
$where
[]
=
'(
LOWER('
.
TB_CHAT
.
'.`title`) LIKE :text
OR LOWER('
.
TB_USER
.
'.`first_name`) LIKE :text
OR LOWER('
.
TB_USER
.
'.`last_name`) LIKE :text
OR LOWER('
.
TB_USER
.
'.`username`) LIKE :text
)'
;
}
else
{
$where
[]
=
'LOWER('
.
TB_CHAT
.
'.`title`) LIKE :text'
;
}
$tokens
[
':text'
]
=
'%'
.
strtolower
(
$text
)
.
'%'
;
}
$a
=
0
;
foreach
(
$where
as
$part
)
{
if
(
$a
)
{
$query
.=
' AND '
.
$part
;
}
else
{
$query
.=
' WHERE '
.
$part
;
++
$a
;
}
if
(
!
empty
(
$where
))
{
$query
.=
' WHERE '
.
implode
(
' AND '
,
$where
);
}
$query
.=
' ORDER BY '
.
TB_CHAT
.
'.`updated_at` ASC'
;
...
...
@@ -1045,11 +1002,9 @@ class DB
$sth
=
self
::
$pdo
->
prepare
(
$query
);
$sth
->
execute
(
$tokens
);
$result
=
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
return
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$result
;
}
}
src/Entities/KeyboardButton.php
View file @
818c3c84
...
...
@@ -35,7 +35,7 @@ class KeyboardButton extends Entity
* @param array $data
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
$data
=
array
()
)
public
function
__construct
(
array
$data
=
[]
)
{
$this
->
text
=
isset
(
$data
[
'text'
])
?
$data
[
'text'
]
:
null
;
if
(
empty
(
$this
->
text
))
{
...
...
src/Entities/ReplyKeyboardMarkup.php
View file @
818c3c84
...
...
@@ -40,27 +40,29 @@ class ReplyKeyboardMarkup extends Entity
* ReplyKeyboardMarkup constructor.
*
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
$data
=
array
()
)
public
function
__construct
(
array
$data
=
[]
)
{
if
(
isset
(
$data
[
'keyboard'
]))
{
if
(
is_array
(
$data
[
'keyboard'
]))
{
if
(
!
isset
(
$data
[
'keyboard'
]))
{
throw
new
TelegramException
(
'Keyboard field is empty!'
);
}
if
(
!
is_array
(
$data
[
'keyboard'
]))
{
throw
new
TelegramException
(
'Keyboard field is not an array!'
);
}
foreach
(
$data
[
'keyboard'
]
as
$item
)
{
if
(
!
is_array
(
$item
))
{
throw
new
TelegramException
(
'Keyboard subfield is not an array!'
);
}
}
$this
->
keyboard
=
$data
[
'keyboard'
];
}
else
{
throw
new
TelegramException
(
'Keyboard field is not an array!'
);
}
}
else
{
throw
new
TelegramException
(
'Keyboard field is empty!'
);
}
$this
->
resize_keyboard
=
isset
(
$data
[
'resize_keyboard'
])
?
$data
[
'resize_keyboard'
]
:
false
;
$this
->
one_time_keyboard
=
isset
(
$data
[
'one_time_keyboard'
])
?
$data
[
'one_time_keyboard'
]
:
false
;
$this
->
selective
=
isset
(
$data
[
'selective'
])
?
$data
[
'selective'
]
:
false
;
//Set the object members from the passed data params
foreach
([
'resize_keyboard'
,
'one_time_keyboard'
,
'selective'
]
as
$param
)
{
$this
->
$param
=
isset
(
$data
[
$param
])
?
(
bool
)
$data
[
$param
]
:
false
;
}
}
}
src/Entities/ServerResponse.php
View file @
818c3c84
...
...
@@ -35,10 +35,12 @@ class ServerResponse extends Entity
*
* @param array $data
* @param $bot_name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
array
$data
,
$bot_name
)
{
if
(
isset
(
$data
[
'ok'
]
)
&&
isset
(
$data
[
'result'
]))
{
if
(
isset
(
$data
[
'ok'
]
,
$data
[
'result'
]))
{
if
(
is_array
(
$data
[
'result'
]))
{
if
(
$data
[
'ok'
]
&&
!
$this
->
isAssoc
(
$data
[
'result'
])
&&
!
isset
(
$data
[
'result'
][
0
][
'user'
]))
{
//Get Update
...
...
@@ -49,7 +51,7 @@ class ServerResponse extends Entity
//Response from getChatAdministrators
$this
->
result
=
[];
foreach
(
$data
[
'result'
]
as
$user
)
{
array_push
(
$this
->
result
,
new
ChatMember
(
$user
)
);
$this
->
result
[]
=
new
ChatMember
(
$user
);
}
}
elseif
(
$data
[
'ok'
]
&&
$this
->
isAssoc
(
$data
[
'result'
]))
{
if
(
isset
(
$data
[
'result'
][
'total_count'
]))
{
...
...
@@ -82,12 +84,7 @@ class ServerResponse extends Entity
$this
->
ok
=
$data
[
'ok'
];
$this
->
result
=
true
;
$this
->
error_code
=
null
;
if
(
isset
(
$data
[
'description'
]))
{
$this
->
description
=
$data
[
'description'
];
}
else
{
$this
->
description
=
''
;
}
$this
->
description
=
isset
(
$data
[
'description'
])
?
$data
[
'description'
]
:
''
;
}
elseif
(
is_numeric
(
$data
[
'result'
]))
{
//Response from getChatMembersCount
$this
->
result
=
$data
[
'result'
];
...
...
@@ -101,24 +98,9 @@ class ServerResponse extends Entity
}
else
{
//webHook not set
$this
->
ok
=
false
;
if
(
isset
(
$data
[
'result'
]))
{
$this
->
result
=
$data
[
'result'
];
}
else
{
$this
->
result
=
null
;
}
if
(
isset
(
$data
[
'error_code'
]))
{
$this
->
error_code
=
$data
[
'error_code'
];
}
else
{
$this
->
error_code
=
null
;
}
if
(
isset
(
$data
[
'description'
]))
{
$this
->
description
=
$data
[
'description'
];
}
else
{
$this
->
description
=
null
;
}
$this
->
result
=
isset
(
$data
[
'result'
])
?
$data
[
'result'
]
:
null
;
$this
->
error_code
=
isset
(
$data
[
'error_code'
])
?
$data
[
'error_code'
]
:
null
;
$this
->
description
=
isset
(
$data
[
'description'
])
?
$data
[
'description'
]
:
null
;
//throw new TelegramException('ok(variable) is not set!');
}
...
...
src/Request.php
View file @
818c3c84
...
...
@@ -87,6 +87,7 @@ class Request
* Initialize
*
* @param \Longman\TelegramBot\Telegram $telegram
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
initialize
(
Telegram
$telegram
)
...
...
@@ -120,6 +121,7 @@ class Request
}
TelegramLog
::
update
(
self
::
$input
);
return
self
::
$input
;
}
...
...
@@ -130,7 +132,7 @@ class Request
*
* @return array Fake response data
*/
public
static
function
generateGeneralFakeServerResponse
(
array
$data
=
null
)
public
static
function
generateGeneralFakeServerResponse
(
array
$data
=
[]
)
{
//PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php
//Maybe this is not the best possible implementation
...
...
@@ -140,7 +142,7 @@ class Request
$fake_response
=
[
'ok'
=>
true
];
// :)
if
(
!
isset
(
$data
)
)
{
if
(
$data
===
[]
)
{
$fake_response
[
'result'
]
=
true
;
}
...
...
@@ -161,57 +163,60 @@ class Request
return
$fake_response
;
}
/**
* Properly set up the request params
*
* If any item of the array is a resource, reformat it to a multipart request.
* Else, just return the passed data as form params.
*
* @param array $data
*
* @return array
*/
private
static
function
setUpRequestParams
(
array
$data
)
{
$has_resource
=
false
;
$multipart
=
[];
//Reformat data array in multipart way if it contains a resource
foreach
(
$data
as
$key
=>
$item
)
{
$has_resource
|=
is_resource
(
$item
);
$multipart
[]
=
[
'name'
=>
$key
,
'contents'
=>
$item
];
}
if
(
$has_resource
)
{
return
[
'multipart'
=>
$multipart
];
}
return
[
'form_params'
=>
$data
];
}
/**
* Execute HTTP Request
*
* @param string $action Action to execute
* @param array
|null
$data Data to attach to the execution
* @param array
$data Data to attach to the execution
*
* @return
mixed
Result of the HTTP Request
* @return
string
Result of the HTTP Request
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
execute
(
$action
,
array
$data
=
null
)
public
static
function
execute
(
$action
,
array
$data
=
[]
)
{
$debug_handle
=
TelegramLog
::
getDebugLogTempStream
();
//Fix so that the keyboard markup is a string, not an object
if
(
isset
(
$data
[
'reply_markup'
])
&&
!
is_string
(
$data
[
'reply_markup'
])
)
{
$data
[
'reply_markup'
]
=
(
string
)
$data
[
'reply_markup'
];
if
(
isset
(
$data
[
'reply_markup'
]))
{
$data
[
'reply_markup'
]
=
(
string
)
$data
[
'reply_markup'
];
}
$request_params
=
[
'debug'
=>
$debug_handle
];
//Check for resources in data
$contains_resource
=
false
;
foreach
(
$data
as
$item
)
{
if
(
is_resource
(
$item
))
{
$contains_resource
=
true
;
break
;
}
}
$request_params
=
self
::
setUpRequestParams
(
$data
);
//Reformat data array in multipart way
if
(
$contains_resource
)
{
foreach
(
$data
as
$key
=>
$item
)
{
$request_params
[
'multipart'
][]
=
array
(
'name'
=>
$key
,
'contents'
=>
$item
);
}
}
else
{
$request_params
[
'form_params'
]
=
$data
;
}
$debug_handle
=
TelegramLog
::
getDebugLogTempStream
();
$request_params
[
'debug'
]
=
$debug_handle
;
try
{
$response
=
self
::
$client
->
post
(
'/bot'
.
self
::
$telegram
->
getApiKey
()
.
'/'
.
$action
,
$request_params
);
}
catch
(
RequestException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
finally
{
//Logging verbose debug output
TelegramLog
::
endDebugLogTempStream
(
"Verbose HTTP Request output:
\n
%s
\n
"
);
}
$result
=
$response
->
getBody
();
$result
=
(
string
)
$response
->
getBody
();
//Logging getUpdates Update
if
(
$action
===
'getUpdates'
)
{
...
...
@@ -219,43 +224,49 @@ class Request
}
return
$result
;
}
catch
(
RequestException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
finally
{
//Logging verbose debug output
TelegramLog
::
endDebugLogTempStream
(
"Verbose HTTP Request output:
\n
%s
\n
"
);
}
}
/**
* Download file
*
* @param Entities\File $file
* @param
\Longman\TelegramBot\
Entities\File $file
*
* @return boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
downloadFile
(
File
$file
)
{
$path
=
$file
->
getFilePath
();
//Create the directory
$loc_path
=
self
::
$telegram
->
getDownloadPath
()
.
'/'
.
$path
;
$tg_file_path
=
$file
->
getFilePath
();
$file_path
=
self
::
$telegram
->
getDownloadPath
()
.
'/'
.
$tg_file_path
;
$dirname
=
dirname
(
$loc_path
);
if
(
!
is_dir
(
$dirname
)
&&
!
mkdir
(
$dirname
,
0755
,
true
))
{
throw
new
TelegramException
(
'Directory '
.
$dirname
.
' can\'t be created'
);
$file_dir
=
dirname
(
$file_path
);
//For safety reasons, first try to create the directory, then check that it exists.
//This is in case some other process has created the folder in the meantime.
if
(
!@
mkdir
(
$file_dir
,
0755
,
true
)
&&
!
is_dir
(
$file_dir
))
{
throw
new
TelegramException
(
'Directory '
.
$file_dir
.
' can\'t be created'
);
}
$debug_handle
=
TelegramLog
::
getDebugLogTempStream
();
try
{
$response
=
self
::
$client
->
get
(
'/file/bot'
.
self
::
$telegram
->
getApiKey
()
.
'/'
.
$path
,
[
'debug'
=>
$debug_handle
,
'sink'
=>
$
loc
_path
]
self
::
$client
->
get
(
'/file/bot'
.
self
::
$telegram
->
getApiKey
()
.
'/'
.
$
tg_file_
path
,
[
'debug'
=>
$debug_handle
,
'sink'
=>
$
file
_path
]
);
return
filesize
(
$file_path
)
>
0
;
}
catch
(
RequestException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
finally
{
//Logging verbose debug output
TelegramLog
::
endDebugLogTempStream
(
"Verbose HTTP File Download Request output:
\n
%s
\n
"
);
}
return
(
filesize
(
$loc_path
)
>
0
);
}
/**
...
...
@@ -272,6 +283,7 @@ class Request
if
(
$fp
===
false
)
{
throw
new
TelegramException
(
'Cannot open '
.
$file
.
' for reading'
);
}
return
$fp
;
}
...
...
@@ -281,37 +293,83 @@ class Request
* @todo Fake response doesn't need json encoding?
*
* @param string $action
* @param array
|null
$data
* @param array
$data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
send
(
$action
,
array
$data
=
null
)
public
static
function
send
(
$action
,
array
$data
=
[]
)
{
if
(
!
in_array
(
$action
,
self
::
$actions
))
{
throw
new
TelegramException
(
'The action '
.
$action
.
' doesn\'t exist!'
);
}
self
::
ensureValidAction
(
$action
);
$bot_name
=
self
::
$telegram
->
getBotName
();
if
(
defined
(
'PHPUNIT_TESTSUITE'
))
{
$fake_response
=
self
::
generateGeneralFakeServerResponse
(
$data
);
return
new
ServerResponse
(
$fake_response
,
$bot_name
);
}
self
::
ensureNonEmptyData
(
$data
);
$response
=
json_decode
(
self
::
execute
(
$action
,
$data
),
true
);
if
(
is_null
(
$response
)
)
{
throw
new
TelegramException
(
'Telegram returned an invalid response! Please
your bot name and api token
.'
);
if
(
null
===
$response
)
{
throw
new
TelegramException
(
'Telegram returned an invalid response! Please
review your bot name and API key
.'
);
}
return
new
ServerResponse
(
$response
,
$bot_name
);
}
/**
* Make sure the data isn't empty, else throw an exception
*
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
static
function
ensureNonEmptyData
(
array
$data
)
{
if
(
count
(
$data
)
===
0
)
{
throw
new
TelegramException
(
'Data is empty!'
);
}
}
/**
* Make sure the action is valid, else throw an exception
*
* @param string $action
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
static
function
ensureValidAction
(
$action
)
{
if
(
!
in_array
(
$action
,
self
::
$actions
,
true
))
{
throw
new
TelegramException
(
'The action " . $action . " doesn\'t exist!'
);
}
}
/**
* Assign an encoded file to a data array
*
* @param array $data
* @param string $field
* @param string $file
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
static
function
assignEncodedFile
(
&
$data
,
$field
,
$file
)
{
if
(
$file
!==
null
&&
$file
!==
''
)
{
$data
[
$field
]
=
self
::
encodeFile
(
$file
);
}
}
/**
* Get me
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
getMe
()
{
...
...
@@ -323,8 +381,6 @@ class Request
/**
* Send message
*
* @todo Could do with some cleaner recursion
*
* @param array $data
*
* @return mixed
...
...
@@ -332,18 +388,18 @@ class Request
*/
public
static
function
sendMessage
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
$text
=
$data
[
'text'
];
$string_len_utf8
=
mb_strlen
(
$text
,
'UTF-8'
);
if
(
$string_len_utf8
>
4096
)
{
do
{
//Chop off and send the first message
$data
[
'text'
]
=
mb_substr
(
$text
,
0
,
4096
);
self
::
send
(
'sendMessage'
,
$data
);
$data
[
'text'
]
=
mb_substr
(
$text
,
4096
,
$string_len_utf8
);
return
self
::
sendMessage
(
$data
);
}
return
self
::
send
(
'sendMessage'
,
$data
);
$response
=
self
::
send
(
'sendMessage'
,
$data
);
//Prepare the next message
$text
=
mb_substr
(
$text
,
4096
);
}
while
(
mb_strlen
(
$text
,
'UTF-8'
)
>
0
);
return
$response
;
}
/**
...
...
@@ -356,10 +412,6 @@ class Request
*/
public
static
function
forwardMessage
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'forwardMessage'
,
$data
);
}
...
...
@@ -374,13 +426,7 @@ class Request
*/
public
static
function
sendPhoto
(
array
$data
,
$file
=
null
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
is_null
(
$file
))
{
$data
[
'photo'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'photo'
,
$file
);
return
self
::
send
(
'sendPhoto'
,
$data
);
}
...
...
@@ -396,13 +442,7 @@ class Request
*/
public
static
function
sendAudio
(
array
$data
,
$file
=
null
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
is_null
(
$file
))
{
$data
[
'audio'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'audio'
,
$file
);
return
self
::
send
(
'sendAudio'
,
$data
);
}
...
...
@@ -418,13 +458,7 @@ class Request
*/
public
static
function
sendDocument
(
array
$data
,
$file
=
null
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
is_null
(
$file
))
{
$data
[
'document'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'document'
,
$file
);
return
self
::
send
(
'sendDocument'
,
$data
);
}
...
...
@@ -440,13 +474,7 @@ class Request
*/
public
static
function
sendSticker
(
array
$data
,
$file
=
null
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
is_null
(
$file
))
{
$data
[
'sticker'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'sticker'
,
$file
);
return
self
::
send
(
'sendSticker'
,
$data
);
}
...
...
@@ -462,13 +490,7 @@ class Request
*/
public
static
function
sendVideo
(
array
$data
,
$file
=
null
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
is_null
(
$file
))
{
$data
[
'video'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'video'
,
$file
);
return
self
::
send
(
'sendVideo'
,
$data
);
}
...
...
@@ -484,13 +506,7 @@ class Request
*/
public
static
function
sendVoice
(
array
$data
,
$file
=
null
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
is_null
(
$file
))
{
$data
[
'voice'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'voice'
,
$file
);
return
self
::
send
(
'sendVoice'
,
$data
);
}
...
...
@@ -505,10 +521,6 @@ class Request
*/
public
static
function
sendLocation
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'sendLocation'
,
$data
);
}
...
...
@@ -522,10 +534,6 @@ class Request
*/
public
static
function
sendVenue
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'sendVenue'
,
$data
);
}
...
...
@@ -539,10 +547,6 @@ class Request
*/
public
static
function
sendContact
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'sendContact'
,
$data
);
}
...
...
@@ -556,10 +560,6 @@ class Request
*/
public
static
function
sendChatAction
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'sendChatAction'
,
$data
);
}
...
...
@@ -573,10 +573,6 @@ class Request
*/
public
static
function
getUserProfilePhotos
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
isset
(
$data
[
'user_id'
]))
{
throw
new
TelegramException
(
'User id is empty!'
);
}
...
...
@@ -590,6 +586,7 @@ class Request
* @param array $data
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
getUpdates
(
array
$data
)
{
...
...
@@ -603,14 +600,13 @@ class Request
* @param string $file
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
setWebhook
(
$url
=
''
,
$file
=
null
)
{
$data
=
[
'url'
=>
$url
];
if
(
!
is_null
(
$file
))
{
$data
[
'certificate'
]
=
self
::
encodeFile
(
$file
);
}
self
::
assignEncodedFile
(
$data
,
'certificate'
,
$file
);
return
self
::
send
(
'setWebhook'
,
$data
);
}
...
...
@@ -625,10 +621,6 @@ class Request
*/
public
static
function
getFile
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'getFile'
,
$data
);
}
...
...
@@ -642,10 +634,6 @@ class Request
*/
public
static
function
kickChatMember
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'kickChatMember'
,
$data
);
}
...
...
@@ -659,10 +647,6 @@ class Request
*/
public
static
function
leaveChat
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'leaveChat'
,
$data
);
}
...
...
@@ -676,10 +660,6 @@ class Request
*/
public
static
function
unbanChatMember
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'unbanChatMember'
,
$data
);
}
...
...
@@ -695,10 +675,6 @@ class Request
*/
public
static
function
getChat
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'getChat'
,
$data
);
}
...
...
@@ -714,10 +690,6 @@ class Request
*/
public
static
function
getChatAdministrators
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'getChatAdministrators'
,
$data
);
}
...
...
@@ -733,10 +705,6 @@ class Request
*/
public
static
function
getChatMembersCount
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'getChatMembersCount'
,
$data
);
}
...
...
@@ -752,10 +720,6 @@ class Request
*/
public
static
function
getChatMember
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'getChatMember'
,
$data
);
}
...
...
@@ -769,10 +733,6 @@ class Request
*/
public
static
function
answerCallbackQuery
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'answerCallbackQuery'
,
$data
);
}
...
...
@@ -786,10 +746,6 @@ class Request
*/
public
static
function
answerInlineQuery
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'answerInlineQuery'
,
$data
);
}
...
...
@@ -803,10 +759,6 @@ class Request
*/
public
static
function
editMessageText
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'editMessageText'
,
$data
);
}
...
...
@@ -820,10 +772,6 @@ class Request
*/
public
static
function
editMessageCaption
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'editMessageCaption'
,
$data
);
}
...
...
@@ -837,10 +785,6 @@ class Request
*/
public
static
function
editMessageReplyMarkup
(
array
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
return
self
::
send
(
'editMessageReplyMarkup'
,
$data
);
}
...
...
@@ -850,7 +794,8 @@ class Request
* No request to telegram are sent, this function is used in commands that
* don't need to fire a message after execution
*
* @return Entities\ServerResponse
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
emptyResponse
()
{
...
...
@@ -888,10 +833,12 @@ class Request
$chats
=
DB
::
selectChats
(
$send_groups
,
$send_super_groups
,
$send_users
,
$date_from
,
$date_to
);
$results
=
[];
if
(
is_array
(
$chats
))
{
foreach
(
$chats
as
$row
)
{
$data
[
'chat_id'
]
=
$row
[
'chat_id'
];
$results
[]
=
call_user_func_array
(
$callback_path
.
'::'
.
$callback_function
,
[
$data
]);
}
}
return
$results
;
}
...
...
src/Telegram.php
View file @
818c3c84
...
...
@@ -18,6 +18,7 @@ use Longman\TelegramBot\Commands\Command;
use
Longman\TelegramBot\Entities\ServerResponse
;
use
Longman\TelegramBot\Entities\Update
;
use
Longman\TelegramBot\Exception\TelegramException
;
use
PDO
;
use
RecursiveDirectoryIterator
;
use
RecursiveIteratorIterator
;
use
RegexIterator
;
...
...
@@ -125,8 +126,9 @@ class Telegram
/**
* Telegram constructor.
*
* @param $api_key
* @param $bot_name
* @param string $api_key
* @param string $bot_name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
$api_key
,
$bot_name
)
...
...
@@ -160,26 +162,33 @@ class Telegram
* @param string $encoding
*
* @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
enableMySql
(
array
$credential
,
$table_prefix
=
null
,
$encoding
=
'utf8mb4'
)
{
$this
->
pdo
=
DB
::
initialize
(
$credential
,
$this
,
$table_prefix
,
$encoding
);
ConversationDB
::
initializeConversation
();
$this
->
mysql_enabled
=
true
;
return
$this
;
}
/**
* Initialize Database external connection
*
* @param
/
PDO $external_pdo_connection PDO database object
* @param PDO $external_pdo_connection PDO database object
* @param string $table_prefix
*
* @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
enableExternalMy
s
ql
(
$external_pdo_connection
,
$table_prefix
=
null
)
public
function
enableExternalMy
S
ql
(
$external_pdo_connection
,
$table_prefix
=
null
)
{
$this
->
pdo
=
DB
::
externalInitialize
(
$external_pdo_connection
,
$this
,
$table_prefix
);
ConversationDB
::
initializeConversation
();
$this
->
mysql_enabled
=
true
;
return
$this
;
}
/**
...
...
@@ -236,7 +245,7 @@ class Telegram
public
function
getCommandObject
(
$command
)
{
$which
=
[
'System'
];
(
$this
->
isAdmin
()
)
&&
$which
[]
=
'Admin'
;
$this
->
isAdmin
(
)
&&
$which
[]
=
'Admin'
;
$which
[]
=
'User'
;
foreach
(
$which
as
$auth
)
{
...
...
@@ -259,6 +268,7 @@ class Telegram
public
function
setCustomInput
(
$input
)
{
$this
->
input
=
$input
;
return
$this
;
}
...
...
@@ -289,6 +299,7 @@ class Telegram
* @param int|null $timeout
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
handleGetUpdates
(
$limit
=
null
,
$timeout
=
null
)
{
...
...
@@ -304,19 +315,23 @@ class Telegram
//DB Query
$last_update
=
DB
::
selectTelegramUpdate
(
1
);
$last_update
=
reset
(
$last_update
);
//As explained in the telegram bot api documentation
$offset
=
(
isset
(
$last_update
[
0
][
'id'
]))
?
$last_update
[
0
]
[
'id'
]
+
1
:
null
;
$offset
=
isset
(
$last_update
[
'id'
])
?
$last_update
[
'id'
]
+
1
:
null
;
$response
=
Request
::
getUpdates
([
$response
=
Request
::
getUpdates
(
[
'offset'
=>
$offset
,
'limit'
=>
$limit
,
'timeout'
=>
$timeout
,
]);
]
);
if
(
$response
->
isOk
())
{
//Process all updates
foreach
((
array
)
$response
->
getResult
()
as
$result
)
{
/** @var Update $result */
foreach
((
array
)
$response
->
getResult
()
as
$result
)
{
$this
->
processUpdate
(
$result
);
}
}
...
...
@@ -338,6 +353,7 @@ class Telegram
if
(
empty
(
$this
->
input
))
{
throw
new
TelegramException
(
'Input is empty!'
);
}
$post
=
json_decode
(
$this
->
input
,
true
);
if
(
empty
(
$post
))
{
throw
new
TelegramException
(
'Invalid JSON!'
);
...
...
@@ -368,6 +384,7 @@ class Telegram
* @param \Longman\TelegramBot\Entities\Update $update
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
processUpdate
(
Update
$update
)
{
...
...
@@ -377,7 +394,7 @@ class Telegram
$command
=
'genericmessage'
;
$update_type
=
$this
->
update
->
getUpdateType
();
if
(
in_array
(
$update_type
,
[
'inline_query'
,
'chosen_inline_result'
,
'callback_query'
,
'edited_message'
]))
{
if
(
in_array
(
$update_type
,
[
'inline_query'
,
'chosen_inline_result'
,
'callback_query'
,
'edited_message'
]
,
true
))
{
$command
=
$this
->
getCommandFromType
(
$update_type
);
}
elseif
(
$update_type
===
'message'
)
{
$message
=
$this
->
update
->
getMessage
();
...
...
@@ -403,7 +420,7 @@ class Telegram
'new_chat_photo'
,
'new_chat_title'
,
'supergroup_chat_created'
,
]))
{
]
,
true
))
{
$command
=
$this
->
getCommandFromType
(
$type
);
}
}
...
...
@@ -477,7 +494,7 @@ class Telegram
*/
public
function
enableAdmin
(
$admin_id
)
{
if
(
is_int
(
$admin_id
)
&&
$admin_id
>
0
&&
!
in_array
(
$admin_id
,
$this
->
admins_list
))
{
if
(
is_int
(
$admin_id
)
&&
$admin_id
>
0
&&
!
in_array
(
$admin_id
,
$this
->
admins_list
,
true
))
{
$this
->
admins_list
[]
=
$admin_id
;
}
else
{
TelegramLog
::
error
(
'Invalid value "'
.
$admin_id
.
'" for admin.'
);
...
...
@@ -524,20 +541,24 @@ class Telegram
public
function
isAdmin
(
$user_id
=
null
)
{
if
(
$user_id
===
null
&&
$this
->
update
!==
null
)
{
if
((
$message
=
$this
->
update
->
getMessage
())
&&
(
$from
=
$message
->
getFrom
()))
{
$user_id
=
$from
->
getId
();
}
elseif
((
$inline_query
=
$this
->
update
->
getInlineQuery
())
&&
(
$from
=
$inline_query
->
getFrom
()))
{
$user_id
=
$from
->
getId
();
}
elseif
((
$chosen_inline_result
=
$this
->
update
->
getChosenInlineResult
())
&&
(
$from
=
$chosen_inline_result
->
getFrom
()))
{
$user_id
=
$from
->
getId
();
}
elseif
((
$callback_query
=
$this
->
update
->
getCallbackQuery
())
&&
(
$from
=
$callback_query
->
getFrom
()))
{
$user_id
=
$from
->
getId
();
}
elseif
((
$edited_message
=
$this
->
update
->
getEditedMessage
())
&&
(
$from
=
$edited_message
->
getFrom
()))
{
//Try to figure out if the user is an admin
$update_methods
=
[
'getMessage'
,
'getInlineQuery'
,
'getChosenInlineResult'
,
'getCallbackQuery'
,
'getEditedMessage'
,
];
foreach
(
$update_methods
as
$update_method
)
{
$object
=
call_user_func
([
$this
->
update
,
$update_method
]);
if
(
$object
!==
null
&&
$from
=
$object
->
getFrom
())
{
$user_id
=
$from
->
getId
();
break
;
}
}
}
return
(
$user_id
===
null
)
?
false
:
in_array
(
$user_id
,
$this
->
admins_list
);
return
(
$user_id
===
null
)
?
false
:
in_array
(
$user_id
,
$this
->
admins_list
,
true
);
}
/**
...
...
@@ -566,11 +587,11 @@ class Telegram
{
if
(
!
is_dir
(
$path
))
{
TelegramLog
::
error
(
'Commands path "'
.
$path
.
'" does not exist.'
);
}
elseif
(
!
in_array
(
$path
,
$this
->
commands_paths
))
{
}
elseif
(
!
in_array
(
$path
,
$this
->
commands_paths
,
true
))
{
if
(
$before
)
{
array_unshift
(
$this
->
commands_paths
,
$path
);
}
else
{
array_push
(
$this
->
commands_paths
,
$path
)
;
$this
->
commands_paths
[]
=
$path
;
}
}
...
...
@@ -588,7 +609,7 @@ class Telegram
public
function
addCommandsPaths
(
array
$paths
,
$before
=
true
)
{
foreach
(
$paths
as
$path
)
{
$this
->
addCommandsPath
(
$path
);
$this
->
addCommandsPath
(
$path
,
$before
);
}
return
$this
;
...
...
@@ -604,6 +625,7 @@ class Telegram
public
function
setUploadPath
(
$path
)
{
$this
->
upload_path
=
$path
;
return
$this
;
}
...
...
@@ -627,6 +649,7 @@ class Telegram
public
function
setDownloadPath
(
$path
)
{
$this
->
download_path
=
$path
;
return
$this
;
}
...
...
@@ -655,6 +678,7 @@ class Telegram
public
function
setCommandConfig
(
$command
,
array
$config
)
{
$this
->
commands_config
[
$command
]
=
$config
;
return
$this
;
}
...
...
@@ -777,12 +801,15 @@ class Telegram
* Enable Botan.io integration
*
* @param $token
*
* @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
enableBotan
(
$token
)
{
Botan
::
initializeBotan
(
$token
);
$this
->
botan_enabled
=
true
;
return
$this
;
}
}
src/TelegramLog.php
View file @
818c3c84
...
...
@@ -22,42 +22,42 @@ class TelegramLog
*
* @var \Monolog\Logger
*/
static
protected
$monolog
=
null
;
static
protected
$monolog
;
/**
* Monolog instance for update
*
* @var \Monolog\Logger
*/
static
protected
$monolog_update
=
null
;
static
protected
$monolog_update
;
/**
* Path for error log
*
* @var string
*/
static
protected
$error_log_path
=
null
;
static
protected
$error_log_path
;
/**
* Path for debug log
*
* @var string
*/
static
protected
$debug_log_path
=
null
;
static
protected
$debug_log_path
;
/**
* Path for update log
*
* @var string
*/
static
protected
$update_log_path
=
null
;
static
protected
$update_log_path
;
/**
* Temporary stream handle for debug log
*
* @var null
*/
static
protected
$debug_log_temp_stream_handle
=
null
;
static
protected
$debug_log_temp_stream_handle
;
/**
* Initialize
...
...
@@ -76,10 +76,10 @@ class TelegramLog
self
::
$monolog
=
$external_monolog
;
foreach
(
self
::
$monolog
->
getHandlers
()
as
$handler
)
{
if
(
$handler
->
getLevel
()
==
400
)
{
if
(
$handler
->
getLevel
()
==
=
400
)
{
self
::
$error_log_path
=
true
;
}
if
(
$handler
->
getLevel
()
==
100
)
{
if
(
$handler
->
getLevel
()
==
=
100
)
{
self
::
$debug_log_path
=
true
;
}
}
...
...
@@ -87,6 +87,7 @@ class TelegramLog
self
::
$monolog
=
new
Logger
(
'bot_log'
);
}
}
return
self
::
$monolog
;
}
...
...
@@ -97,6 +98,8 @@ class TelegramLog
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
*/
public
static
function
initErrorLog
(
$path
)
{
...
...
@@ -119,6 +122,8 @@ class TelegramLog
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
*/
public
static
function
initDebugLog
(
$path
)
{
...
...
@@ -148,6 +153,7 @@ class TelegramLog
return
false
;
}
}
return
self
::
$debug_log_temp_stream_handle
;
}
...
...
@@ -181,6 +187,8 @@ class TelegramLog
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
*/
public
static
function
initUpdateLog
(
$path
)
{
...
...
@@ -200,6 +208,7 @@ class TelegramLog
self
::
$monolog_update
->
pushHandler
(
$update_handler
);
}
return
self
::
$monolog
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment