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
34ad296b
Commit
34ad296b
authored
Dec 26, 2016
by
Jack'lul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Botan integration improvements
parent
1c54e2c0
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
199 additions
and
97 deletions
+199
-97
ShortenerCommand.php
examples/Commands/ShortenerCommand.php
+49
-0
getUpdatesCLI.php
examples/getUpdatesCLI.php
+2
-0
hook.php
examples/hook.php
+2
-0
Botan.php
src/Botan.php
+131
-91
BotanDB.php
src/BotanDB.php
+10
-2
Telegram.php
src/Telegram.php
+5
-4
No files found.
examples/Commands/ShortenerCommand.php
0 → 100644
View file @
34ad296b
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Longman\TelegramBot\Commands\UserCommands
;
use
Longman\TelegramBot\Botan
;
use
Longman\TelegramBot\Commands\UserCommand
;
use
Longman\TelegramBot\Request
;
/**
* User "/shortener" command
*/
class
ShortenerCommand
extends
UserCommand
{
/**#@+
* {@inheritdoc}
*/
protected
$name
=
'shortener'
;
protected
$description
=
'Botan Shortener example'
;
protected
$usage
=
'/shortener'
;
protected
$version
=
'1.0.0'
;
/**#@-*/
/**
* {@inheritdoc}
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$user_id
=
$message
->
getFrom
()
->
getId
();
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$text
=
Botan
::
shortenUrl
(
"https://github.com/akalongman/php-telegram-bot"
,
$user_id
);
$data
[
'text'
]
=
$text
;
return
Request
::
sendMessage
(
$data
);
}
}
examples/getUpdatesCLI.php
View file @
34ad296b
...
@@ -62,7 +62,9 @@ try {
...
@@ -62,7 +62,9 @@ try {
//$telegram->setUploadPath('../Upload');
//$telegram->setUploadPath('../Upload');
// Botan.io integration
// Botan.io integration
// Second argument is optional maximum timeout
//$telegram->enableBotan('your_token');
//$telegram->enableBotan('your_token');
//$telegram->enableBotan('your_token', 3);
// Handle telegram getUpdates request
// Handle telegram getUpdates request
$serverResponse
=
$telegram
->
handleGetUpdates
();
$serverResponse
=
$telegram
->
handleGetUpdates
();
...
...
examples/hook.php
View file @
34ad296b
...
@@ -61,7 +61,9 @@ try {
...
@@ -61,7 +61,9 @@ try {
//$telegram->setUploadPath('../Upload');
//$telegram->setUploadPath('../Upload');
// Botan.io integration
// Botan.io integration
// Second argument is optional maximum timeout
//$telegram->enableBotan('your_token');
//$telegram->enableBotan('your_token');
//$telegram->enableBotan('your_token', 3);
// Handle telegram webhook request
// Handle telegram webhook request
$telegram
->
handle
();
$telegram
->
handle
();
...
...
src/Botan.php
View file @
34ad296b
...
@@ -10,6 +10,9 @@
...
@@ -10,6 +10,9 @@
namespace
Longman\TelegramBot
;
namespace
Longman\TelegramBot
;
use
GuzzleHttp\Client
;
use
GuzzleHttp\Exception\RequestException
;
use
Longman\TelegramBot\Entities\Update
;
use
Longman\TelegramBot\Exception\TelegramException
;
use
Longman\TelegramBot\Exception\TelegramException
;
/**
/**
...
@@ -20,82 +23,107 @@ use Longman\TelegramBot\Exception\TelegramException;
...
@@ -20,82 +23,107 @@ use Longman\TelegramBot\Exception\TelegramException;
class
Botan
class
Botan
{
{
/**
/**
* @var string Tracker request url
* Botan.io API URL
*
* @var string
*/
*/
protected
static
$
track_url
=
'https://api.botan.io/track?token=#TOKEN&uid=#UID&name=#NAME
'
;
protected
static
$
api_base_uri
=
'https://api.botan.io
'
;
/**
/**
* @var string Url Shortener request url
* Yandex AppMetrica application key
*
* @var string
*/
*/
protected
static
$
shortener_url
=
'https://api.botan.io/s/?token=#TOKEN&user_ids=#UID&url=#URL
'
;
protected
static
$
token
=
'
'
;
/**
/**
* @var string Yandex AppMetrica application key
* Guzzle Client object
*
* @var \GuzzleHttp\Client
*/
*/
pr
otected
static
$token
=
''
;
pr
ivate
static
$client
;
/**
/**
* @var string The actual command that is going to be reported
* The actual command that is going to be reported
*
* Set as public to let the developers either:
* - block tracking from inside commands by setting the value to non-existent command
* - override which command is tracked when commands call other commands with executedCommand()
*
* @var string
*/
*/
public
static
$command
=
''
;
public
static
$command
=
''
;
/**
/**
* Initialize Botan
* Initialize Botan
*
*
* @param $token
* @param string $token
* @param integer $timeout
*
* @throws \Longman\TelegramBot\Exception\TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
*/
public
static
function
initializeBotan
(
$token
)
public
static
function
initializeBotan
(
$token
,
$timeout
=
3
)
{
{
if
(
empty
(
$token
)
||
!
is_string
(
$token
))
{
if
(
empty
(
$token
))
{
throw
new
TelegramException
(
'Botan token should be a string!'
);
throw
new
TelegramException
(
'Botan token is empty!'
);
}
if
(
!
is_numeric
(
$timeout
))
{
throw
new
TelegramException
(
'Timeout must be a number!'
);
}
}
self
::
$token
=
$token
;
self
::
$token
=
$token
;
self
::
$client
=
new
Client
([
'base_uri'
=>
self
::
$api_base_uri
,
'timeout'
=>
$timeout
]);
BotanDB
::
initializeBotanDb
();
BotanDB
::
initializeBotanDb
();
}
}
/**
/**
* Lock function to make sure only the first command is reported
* Lock function to make sure only the first command is reported (the one user requested)
* ( in case commands are calling other commands $telegram->executedCommand() )
*
*
* @param string $command
* This is in case commands are calling other commands with executedCommand()
*
* @param string $command
*/
*/
public
static
function
lock
(
$command
=
''
)
public
static
function
lock
(
$command
=
''
)
{
{
if
(
empty
(
self
::
$command
))
{
if
(
empty
(
self
::
$command
))
{
self
::
$command
=
$command
;
self
::
$command
=
strtolower
(
$command
)
;
}
}
}
}
/**
/**
* Track function
* Track function
*
*
* @param
string $input
* @param
\Longman\TelegramBot\Entities\Update $update
* @param string $command
* @param string $command
*
*
* @return bool|string
* @return bool|string
* @throws \Longman\TelegramBot\Exception\TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
*/
public
static
function
track
(
$input
,
$command
=
''
)
public
static
function
track
(
Update
$update
,
$command
=
''
)
{
{
if
(
empty
(
self
::
$token
)
||
$command
!==
self
::
$command
)
{
if
(
empty
(
self
::
$token
)
||
strtolower
(
$command
)
!==
self
::
$command
)
{
return
false
;
return
false
;
}
}
if
(
empty
(
$
input
))
{
if
(
empty
(
$
update
))
{
throw
new
TelegramException
(
'
Inpu
t is empty!'
);
throw
new
TelegramException
(
'
Update objec
t is empty!'
);
}
}
// Release the lock in case someone runs getUpdates in foreach loop
self
::
$command
=
''
;
self
::
$command
=
''
;
$obj
=
json_decode
(
$input
,
true
);
// For now, this is the only way
$update_data
=
(
array
)
$update
;
$data
=
[];
$data
=
[];
if
(
isset
(
$obj
[
'message'
]
))
{
if
(
$update
->
getMessage
(
))
{
$data
=
$
obj
[
'message'
];
$data
=
$
update_data
[
'message'
];
$event_name
=
'Message'
;
$event_name
=
'
Generic
Message'
;
if
(
isset
(
$obj
[
'message'
][
'entities'
])
&&
is_array
(
$obj
[
'message'
]
[
'entities'
]))
{
if
(
!
empty
(
$data
[
'entities'
])
&&
is_array
(
$data
[
'entities'
]))
{
foreach
(
$
obj
[
'message'
]
[
'entities'
]
as
$entity
)
{
foreach
(
$
data
[
'entities'
]
as
$entity
)
{
if
(
$entity
[
'type'
]
===
'bot_command'
&&
$entity
[
'offset'
]
===
0
)
{
if
(
$entity
[
'type'
]
===
'bot_command'
&&
$entity
[
'offset'
]
===
0
)
{
if
(
strtolower
(
$command
)
===
'generic'
)
{
if
(
strtolower
(
$command
)
===
'generic'
)
{
$command
=
'Generic'
;
$command
=
'Generic'
;
...
@@ -110,23 +138,23 @@ class Botan
...
@@ -110,23 +138,23 @@ class Botan
}
}
}
}
}
}
}
elseif
(
isset
(
$obj
[
'edited_message'
]
))
{
}
elseif
(
$update
->
getEditedMessage
(
))
{
$data
=
$
obj
[
'edited_message'
];
$data
=
$
update_data
[
'edited_message'
];
$event_name
=
'Edited Message'
;
$event_name
=
'Edited Message'
;
}
elseif
(
isset
(
$obj
[
'channel_post'
]
))
{
}
elseif
(
$update
->
getChannelPost
(
))
{
$data
=
$
obj
[
'channel_post'
];
$data
=
$
update_data
[
'channel_post'
];
$event_name
=
'Channel
Message
'
;
$event_name
=
'Channel
Post
'
;
}
elseif
(
isset
(
$obj
[
'edited_channel_post'
]
))
{
}
elseif
(
$update
->
getEditedChannelPost
(
))
{
$data
=
$
obj
[
'edited_channel_post'
];
$data
=
$
update_data
[
'edited_channel_post'
];
$event_name
=
'Edited Channel
Message
'
;
$event_name
=
'Edited Channel
Post
'
;
}
elseif
(
isset
(
$obj
[
'inline_query'
]
))
{
}
elseif
(
$update
->
getInlineQuery
(
))
{
$data
=
$
obj
[
'inline_query'
];
$data
=
$
update_data
[
'inline_query'
];
$event_name
=
'Inline Query'
;
$event_name
=
'Inline Query'
;
}
elseif
(
isset
(
$obj
[
'chosen_inline_result'
]
))
{
}
elseif
(
$update
->
getChosenInlineResult
(
))
{
$data
=
$
obj
[
'chosen_inline_result'
];
$data
=
$
update_data
[
'chosen_inline_result'
];
$event_name
=
'Chosen Inline Result'
;
$event_name
=
'Chosen Inline Result'
;
}
elseif
(
isset
(
$obj
[
'callback_query'
]
))
{
}
elseif
(
$update
->
getCallbackQuery
(
))
{
$data
=
$
obj
[
'callback_query'
];
$data
=
$
update_data
[
'callback_query'
];
$event_name
=
'Callback Query'
;
$event_name
=
'Callback Query'
;
}
}
...
@@ -134,38 +162,51 @@ class Botan
...
@@ -134,38 +162,51 @@ class Botan
return
false
;
return
false
;
}
}
$uid
=
$data
[
'from'
][
'id'
];
// In case there is no from field (channel posts) assign chat id
$request
=
str_replace
(
if
(
isset
(
$data
[
'from'
][
'id'
]))
{
[
'#TOKEN'
,
'#UID'
,
'#NAME'
],
$uid
=
$data
[
'from'
][
'id'
];
[
self
::
$token
,
$uid
,
urlencode
(
$event_name
)],
}
elseif
(
isset
(
$data
[
'chat'
][
'id'
]))
{
self
::
$track_url
$uid
=
$data
[
'chat'
][
'id'
];
);
}
else
{
$uid
=
0
;
// if that fails too assign id = 0
$options
=
[
'http'
=>
[
'header'
=>
'Content-Type: application/json'
,
'method'
=>
'POST'
,
'content'
=>
json_encode
(
$data
),
'ignore_errors'
=>
true
,
],
];
$context
=
stream_context_create
(
$options
);
$response
=
@
file_get_contents
(
$request
,
false
,
$context
);
$responseData
=
json_decode
(
$response
,
true
);
if
(
$responseData
[
'status'
]
!==
'accepted'
)
{
TelegramLog
::
debug
(
'Botan.io API replied with error: '
.
$response
);
}
}
return
$responseData
;
try
{
$response
=
self
::
$client
->
request
(
'POST'
,
str_replace
(
[
'#TOKEN'
,
'#UID'
,
'#NAME'
],
[
self
::
$token
,
$uid
,
urlencode
(
$event_name
)],
'/track?token=#TOKEN&uid=#UID&name=#NAME'
),
[
'headers'
=>
[
'Content-Type'
=>
'application/json'
],
'json'
=>
$data
]
);
$response
=
(
string
)
$response
->
getBody
();
}
catch
(
RequestException
$e
)
{
$response
=
(
$e
->
getResponse
())
?
(
string
)
$e
->
getResponse
()
->
getBody
()
:
''
;
}
finally
{
$responseData
=
json_decode
(
$response
,
true
);
if
(
$responseData
[
'status'
]
!==
'accepted'
)
{
TelegramLog
::
debug
(
"Botan.io API replied with error:
\n
$response
\n\n
"
);
return
false
;
}
return
$responseData
;
}
}
}
/**
/**
* Url Shortener function
* Url Shortener function
*
*
* @param $url
* @param
string
$url
* @param $user_id
* @param
integer
$user_id
*
*
* @return string
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
...
@@ -181,33 +222,32 @@ class Botan
...
@@ -181,33 +222,32 @@ class Botan
}
}
$cached
=
BotanDB
::
selectShortUrl
(
$user_id
,
$url
);
$cached
=
BotanDB
::
selectShortUrl
(
$user_id
,
$url
);
if
(
!
empty
(
$cached
[
0
][
'short_url'
]))
{
return
$cached
[
0
][
'short_url'
];
}
$request
=
str_replace
(
[
'#TOKEN'
,
'#UID'
,
'#URL'
],
[
self
::
$token
,
$user_id
,
urlencode
(
$url
)],
self
::
$shortener_url
);
$options
=
[
if
(
!
empty
(
$cached
))
{
'http'
=>
[
return
$cached
;
'ignore_errors'
=>
true
,
'timeout'
=>
3
,
],
];
$context
=
stream_context_create
(
$options
);
$response
=
@
file_get_contents
(
$request
,
false
,
$context
);
if
(
!
filter_var
(
$response
,
FILTER_VALIDATE_URL
)
===
false
)
{
BotanDB
::
insertShortUrl
(
$user_id
,
$url
,
$response
);
}
else
{
TelegramLog
::
debug
(
'Botan.io API replied with error: '
.
$response
);
return
$url
;
}
}
return
$response
;
try
{
$response
=
self
::
$client
->
request
(
'POST'
,
str_replace
(
[
'#TOKEN'
,
'#UID'
,
'#URL'
],
[
self
::
$token
,
$user_id
,
urlencode
(
$url
)],
'/s/?token=#TOKEN&user_ids=#UID&url=#URL'
)
);
$response
=
(
string
)
$response
->
getBody
();
}
catch
(
RequestException
$e
)
{
$response
=
(
$e
->
getResponse
())
?
(
string
)
$e
->
getResponse
()
->
getBody
()
:
''
;
}
finally
{
if
(
!
filter_var
(
$response
,
FILTER_VALIDATE_URL
)
===
false
)
{
BotanDB
::
insertShortUrl
(
$user_id
,
$url
,
$response
);
return
$response
;
}
else
{
TelegramLog
::
error
(
"Botan.io URL shortening failed - API replied with error:
\n
$response
\n\n
"
);
return
$url
;
}
}
}
}
}
}
src/BotanDB.php
View file @
34ad296b
...
@@ -47,13 +47,21 @@ class BotanDB extends DB
...
@@ -47,13 +47,21 @@ class BotanDB extends DB
try
{
try
{
$sth
=
self
::
$pdo
->
prepare
(
'SELECT * FROM `'
.
TB_BOTAN_SHORTENER
.
'`
$sth
=
self
::
$pdo
->
prepare
(
'SELECT * FROM `'
.
TB_BOTAN_SHORTENER
.
'`
WHERE `user_id` = :user_id AND `url` = :url
WHERE `user_id` = :user_id AND `url` = :url
ORDER BY `created_at` DESC
LIMIT 1
'
);
'
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':url'
,
$url
,
PDO
::
PARAM_
INT
);
$sth
->
bindParam
(
':url'
,
$url
,
PDO
::
PARAM_
STR
);
$sth
->
execute
();
$sth
->
execute
();
return
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
$result
=
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
if
(
isset
(
$result
[
0
][
'short_url'
]))
{
return
$result
[
0
][
'short_url'
];
}
return
$result
;
}
catch
(
Exception
$e
)
{
}
catch
(
Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
throw
new
TelegramException
(
$e
->
getMessage
());
}
}
...
...
src/Telegram.php
View file @
34ad296b
...
@@ -455,7 +455,7 @@ class Telegram
...
@@ -455,7 +455,7 @@ class Telegram
//Handle a generic command or non existing one
//Handle a generic command or non existing one
$this
->
last_command_response
=
$this
->
executeCommand
(
'Generic'
);
$this
->
last_command_response
=
$this
->
executeCommand
(
'Generic'
);
}
else
{
}
else
{
//Botan.io integration, make sure only the command user executed is reported
//Botan.io integration, make sure only the
actual
command user executed is reported
if
(
$this
->
botan_enabled
)
{
if
(
$this
->
botan_enabled
)
{
Botan
::
lock
(
$command
);
Botan
::
lock
(
$command
);
}
}
...
@@ -815,14 +815,15 @@ class Telegram
...
@@ -815,14 +815,15 @@ class Telegram
/**
/**
* Enable Botan.io integration
* Enable Botan.io integration
*
*
* @param $token
* @param string $token
* @param integer $timeout
*
*
* @return \Longman\TelegramBot\Telegram
* @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
*/
public
function
enableBotan
(
$token
)
public
function
enableBotan
(
$token
,
$timeout
=
3
)
{
{
Botan
::
initializeBotan
(
$token
);
Botan
::
initializeBotan
(
$token
,
$timeout
);
$this
->
botan_enabled
=
true
;
$this
->
botan_enabled
=
true
;
return
$this
;
return
$this
;
...
...
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