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 {
//$telegram->setUploadPath('../Upload');
// Botan.io integration
// Second argument is optional maximum timeout
//$telegram->enableBotan('your_token');
//$telegram->enableBotan('your_token', 3);
// Handle telegram getUpdates request
$serverResponse
=
$telegram
->
handleGetUpdates
();
...
...
examples/hook.php
View file @
34ad296b
...
...
@@ -61,7 +61,9 @@ try {
//$telegram->setUploadPath('../Upload');
// Botan.io integration
// Second argument is optional maximum timeout
//$telegram->enableBotan('your_token');
//$telegram->enableBotan('your_token', 3);
// Handle telegram webhook request
$telegram
->
handle
();
...
...
src/Botan.php
View file @
34ad296b
...
...
@@ -10,6 +10,9 @@
namespace
Longman\TelegramBot
;
use
GuzzleHttp\Client
;
use
GuzzleHttp\Exception\RequestException
;
use
Longman\TelegramBot\Entities\Update
;
use
Longman\TelegramBot\Exception\TelegramException
;
/**
...
...
@@ -20,82 +23,107 @@ use Longman\TelegramBot\Exception\TelegramException;
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
=
''
;
/**
* Initialize Botan
*
* @param $token
* @param string $token
* @param integer $timeout
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
initializeBotan
(
$token
)
public
static
function
initializeBotan
(
$token
,
$timeout
=
3
)
{
if
(
empty
(
$token
)
||
!
is_string
(
$token
))
{
throw
new
TelegramException
(
'Botan token should be a string!'
);
if
(
empty
(
$token
))
{
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
();
}
/**
* Lock function to make sure only the first command is reported
* ( in case commands are calling other commands $telegram->executedCommand() )
* Lock function to make sure only the first command is reported (the one user requested)
*
* @param string $command
* This is in case commands are calling other commands with executedCommand()
*
* @param string $command
*/
public
static
function
lock
(
$command
=
''
)
{
if
(
empty
(
self
::
$command
))
{
self
::
$command
=
$command
;
self
::
$command
=
strtolower
(
$command
)
;
}
}
/**
* Track function
*
* @param
string $input
* @param
\Longman\TelegramBot\Entities\Update $update
* @param string $command
*
* @return bool|string
* @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
;
}
if
(
empty
(
$
input
))
{
throw
new
TelegramException
(
'
Inpu
t is empty!'
);
if
(
empty
(
$
update
))
{
throw
new
TelegramException
(
'
Update objec
t is empty!'
);
}
// Release the lock in case someone runs getUpdates in foreach loop
self
::
$command
=
''
;
$obj
=
json_decode
(
$input
,
true
);
// For now, this is the only way
$update_data
=
(
array
)
$update
;
$data
=
[];
if
(
isset
(
$obj
[
'message'
]
))
{
$data
=
$
obj
[
'message'
];
$event_name
=
'Message'
;
if
(
$update
->
getMessage
(
))
{
$data
=
$
update_data
[
'message'
];
$event_name
=
'
Generic
Message'
;
if
(
isset
(
$obj
[
'message'
][
'entities'
])
&&
is_array
(
$obj
[
'message'
]
[
'entities'
]))
{
foreach
(
$
obj
[
'message'
]
[
'entities'
]
as
$entity
)
{
if
(
!
empty
(
$data
[
'entities'
])
&&
is_array
(
$data
[
'entities'
]))
{
foreach
(
$
data
[
'entities'
]
as
$entity
)
{
if
(
$entity
[
'type'
]
===
'bot_command'
&&
$entity
[
'offset'
]
===
0
)
{
if
(
strtolower
(
$command
)
===
'generic'
)
{
$command
=
'Generic'
;
...
...
@@ -110,23 +138,23 @@ class Botan
}
}
}
}
elseif
(
isset
(
$obj
[
'edited_message'
]
))
{
$data
=
$
obj
[
'edited_message'
];
}
elseif
(
$update
->
getEditedMessage
(
))
{
$data
=
$
update_data
[
'edited_message'
];
$event_name
=
'Edited Message'
;
}
elseif
(
isset
(
$obj
[
'channel_post'
]
))
{
$data
=
$
obj
[
'channel_post'
];
$event_name
=
'Channel
Message
'
;
}
elseif
(
isset
(
$obj
[
'edited_channel_post'
]
))
{
$data
=
$
obj
[
'edited_channel_post'
];
$event_name
=
'Edited Channel
Message
'
;
}
elseif
(
isset
(
$obj
[
'inline_query'
]
))
{
$data
=
$
obj
[
'inline_query'
];
}
elseif
(
$update
->
getChannelPost
(
))
{
$data
=
$
update_data
[
'channel_post'
];
$event_name
=
'Channel
Post
'
;
}
elseif
(
$update
->
getEditedChannelPost
(
))
{
$data
=
$
update_data
[
'edited_channel_post'
];
$event_name
=
'Edited Channel
Post
'
;
}
elseif
(
$update
->
getInlineQuery
(
))
{
$data
=
$
update_data
[
'inline_query'
];
$event_name
=
'Inline Query'
;
}
elseif
(
isset
(
$obj
[
'chosen_inline_result'
]
))
{
$data
=
$
obj
[
'chosen_inline_result'
];
}
elseif
(
$update
->
getChosenInlineResult
(
))
{
$data
=
$
update_data
[
'chosen_inline_result'
];
$event_name
=
'Chosen Inline Result'
;
}
elseif
(
isset
(
$obj
[
'callback_query'
]
))
{
$data
=
$
obj
[
'callback_query'
];
}
elseif
(
$update
->
getCallbackQuery
(
))
{
$data
=
$
update_data
[
'callback_query'
];
$event_name
=
'Callback Query'
;
}
...
...
@@ -134,38 +162,51 @@ class Botan
return
false
;
}
$uid
=
$data
[
'from'
][
'id'
];
$request
=
str_replace
(
[
'#TOKEN'
,
'#UID'
,
'#NAME'
],
[
self
::
$token
,
$uid
,
urlencode
(
$event_name
)],
self
::
$track_url
);
$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
);
// In case there is no from field (channel posts) assign chat id
if
(
isset
(
$data
[
'from'
][
'id'
]))
{
$uid
=
$data
[
'from'
][
'id'
];
}
elseif
(
isset
(
$data
[
'chat'
][
'id'
]))
{
$uid
=
$data
[
'chat'
][
'id'
];
}
else
{
$uid
=
0
;
// if that fails too assign id = 0
}
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
*
* @param $url
* @param $user_id
* @param
string
$url
* @param
integer
$user_id
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
...
...
@@ -181,33 +222,32 @@ class Botan
}
$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
=
[
'http'
=>
[
'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
;
if
(
!
empty
(
$cached
))
{
return
$cached
;
}
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
try
{
$sth
=
self
::
$pdo
->
prepare
(
'SELECT * FROM `'
.
TB_BOTAN_SHORTENER
.
'`
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
(
':url'
,
$url
,
PDO
::
PARAM_
INT
);
$sth
->
bindParam
(
':url'
,
$url
,
PDO
::
PARAM_
STR
);
$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
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
src/Telegram.php
View file @
34ad296b
...
...
@@ -455,7 +455,7 @@ class Telegram
//Handle a generic command or non existing one
$this
->
last_command_response
=
$this
->
executeCommand
(
'Generic'
);
}
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
)
{
Botan
::
lock
(
$command
);
}
...
...
@@ -815,14 +815,15 @@ class Telegram
/**
* Enable Botan.io integration
*
* @param $token
* @param string $token
* @param integer $timeout
*
* @return \Longman\TelegramBot\Telegram
* @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
;
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