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
771a5061
Commit
771a5061
authored
May 20, 2016
by
Jack'lul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
introduce botan.io integration
parent
c87ae13e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
318 additions
and
0 deletions
+318
-0
getUpdatesCLI.php
examples/getUpdatesCLI.php
+3
-0
hook.php
examples/hook.php
+3
-0
Botan.php
src/Botan.php
+174
-0
BotanDB.php
src/BotanDB.php
+101
-0
Telegram.php
src/Telegram.php
+26
-0
structure.sql
structure.sql
+11
-0
No files found.
examples/getUpdatesCLI.php
View file @
771a5061
...
...
@@ -50,6 +50,9 @@ try {
//$telegram->setDownloadPath('../Download');
//$telegram->setUploadPath('../Upload');
//// Botan.io integration
//$telegram->enableBotan('your_token');
// Handle telegram getUpdate request
$ServerResponse
=
$telegram
->
handleGetUpdates
();
...
...
examples/hook.php
View file @
771a5061
...
...
@@ -48,6 +48,9 @@ try {
//$telegram->setDownloadPath('../Download');
//$telegram->setUploadPath('../Upload');
//// Botan.io integration
//$telegram->enableBotan('your_token');
// Handle telegram webhook request
$telegram
->
handle
();
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
...
...
src/Botan.php
0 → 100644
View file @
771a5061
<?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
;
use
Longman\TelegramBot\Exception\TelegramException
;
/**
* Class Botan
*
* Integration with http://botan.io statistics service for Telegram bots
*/
class
Botan
{
/**
* @var string Tracker request url
*/
protected
static
$track_url
=
'https://api.botan.io/track?token=#TOKEN&uid=#UID&name=#NAME'
;
/**
* @var string Url Shortener request url
*/
protected
static
$shortener_url
=
'https://api.botan.io/s/?token=#TOKEN&user_ids=#UID&url=#URL'
;
/**
* @var string Yandex AppMetrica application key
*/
protected
static
$token
=
''
;
/**
* Initilize botan
*/
public
static
function
initializeBotan
(
$token
)
{
if
(
empty
(
$token
)
||
!
is_string
(
$token
))
{
throw
new
TelegramException
(
'Botan token should be a string!'
);
}
self
::
$token
=
$token
;
BotanDB
::
initializeBotanDb
();
}
/**
* Track function
*
* @todo Advanced integration: https://github.com/botanio/sdk#advanced-integration
*
* @param string $input
* @param string $command
* @return bool|string
*/
public
static
function
track
(
$input
,
$command
=
''
)
{
if
(
empty
(
self
::
$token
))
{
return
false
;
}
if
(
empty
(
$input
))
{
throw
new
TelegramException
(
'Input is empty!'
);
}
$obj
=
json_decode
(
$input
,
true
);
if
(
isset
(
$obj
[
'message'
]))
{
$data
=
$obj
[
'message'
];
if
((
isset
(
$obj
[
'message'
][
'entities'
])
&&
$obj
[
'message'
][
'entities'
][
0
][
'type'
]
==
'bot_command'
)
||
substr
(
$obj
[
'message'
][
'text'
],
0
,
1
)
==
'/'
)
{
if
(
strtolower
(
$command
)
==
'generic'
)
{
$command
=
'Generic'
;
}
elseif
(
strtolower
(
$command
)
==
'genericmessage'
)
{
$command
=
'Generic Message'
;
}
else
{
$command
=
'/'
.
strtolower
(
$command
);
}
$event_name
=
'Command ('
.
$command
.
')'
;
}
else
{
$event_name
=
'Message'
;
}
}
elseif
(
isset
(
$obj
[
'inline_query'
]))
{
$data
=
$obj
[
'inline_query'
];
$event_name
=
'Inline Query'
;
}
elseif
(
isset
(
$obj
[
'chosen_inline_result'
]))
{
$data
=
$obj
[
'chosen_inline_result'
];
$event_name
=
'Chosen Inline Result'
;
}
elseif
(
isset
(
$obj
[
'callback_query'
]))
{
$data
=
$obj
[
'callback_query'
];
$event_name
=
'Callback Query'
;
}
if
(
empty
(
$event_name
))
{
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'
)
{
error_log
(
'Botan.io API replied with error: '
.
$response
);
}
return
$responseData
;
}
/**
* Url Shortener function
*
* @param $url
* @param $user_id
* @return string
*/
public
static
function
shortenUrl
(
$url
,
$user_id
)
{
if
(
empty
(
self
::
$token
))
{
return
$url
;
}
if
(
empty
(
$user_id
))
{
throw
new
TelegramException
(
'User id is empty!'
);
}
$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
]
];
$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
{
error_log
(
'Botan.io API replied with error: '
.
$response
);
return
$url
;
}
return
$response
;
}
}
src/BotanDB.php
0 → 100644
View file @
771a5061
<?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
;
use
Longman\TelegramBot\DB
;
use
Longman\TelegramBot\Exception\TelegramException
;
/**
* Class BotanDB
*/
class
BotanDB
extends
DB
{
/**
* Initilize botan shortener table
*/
public
static
function
initializeBotanDb
()
{
if
(
!
defined
(
'TB_BOTAN_SHORTENER'
))
{
define
(
'TB_BOTAN_SHORTENER'
,
self
::
$table_prefix
.
'botan_shortener'
);
}
}
/**
* Select cached shortened URL from the database
*
* @param $user_id
* @param $url
* @return bool|string
* @throws TelegramException
*/
public
static
function
selectShortUrl
(
$user_id
,
$url
)
{
if
(
!
self
::
isDbConnected
())
{
return
false
;
}
try
{
$sth
=
self
::
$pdo
->
prepare
(
'SELECT * FROM `'
.
TB_BOTAN_SHORTENER
.
'`
WHERE `user_id` = :user_id AND `url` = :url
'
);
$sth
->
bindParam
(
':user_id'
,
$user_id
,
\PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':url'
,
$url
,
\PDO
::
PARAM_INT
);
$sth
->
execute
();
$results
=
$sth
->
fetchAll
(
\PDO
::
FETCH_ASSOC
);
}
catch
(
\Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$results
;
}
/**
* Insert shortened URL into the database
*
* @param $user_id
* @param $url
* @param $short_url
* @return bool
* @throws TelegramException
*/
public
static
function
insertShortUrl
(
$user_id
,
$url
,
$short_url
)
{
if
(
!
self
::
isDbConnected
())
{
return
false
;
}
try
{
$sth
=
self
::
$pdo
->
prepare
(
'INSERT INTO `'
.
TB_BOTAN_SHORTENER
.
'`
(
`user_id`, `url`, `short_url`, `created_at`
)
VALUES (
:user_id, :url, :short_url, :date
)
'
);
$created_at
=
self
::
getTimestamp
();
$sth
->
bindParam
(
':user_id'
,
$user_id
);
$sth
->
bindParam
(
':url'
,
$url
);
$sth
->
bindParam
(
':short_url'
,
$short_url
);
$sth
->
bindParam
(
':date'
,
$created_at
);
$status
=
$sth
->
execute
();
}
catch
(
\Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
return
$status
;
}
}
src/Telegram.php
View file @
771a5061
...
...
@@ -137,6 +137,13 @@ class Telegram
*/
protected
$last_command_response
;
/**
* Botan.io integration
*
* @var boolean
*/
protected
$botan_enabled
=
false
;
/**
* Constructor
*
...
...
@@ -511,6 +518,11 @@ class Telegram
//execute() method is executed after preExecute()
//This is to prevent executing a DB query without a valid connection
$this
->
last_command_response
=
$command_obj
->
preExecute
();
//Botan.io integration
if
(
$this
->
botan_enabled
)
{
Botan
::
track
(
$this
->
input
,
$command
);
}
}
return
$this
->
last_command_response
;
...
...
@@ -791,4 +803,18 @@ class Telegram
{
return
mb_strtoupper
(
mb_substr
(
$str
,
0
,
1
,
$encoding
),
$encoding
)
.
mb_strtolower
(
mb_substr
(
$str
,
1
,
mb_strlen
(
$str
),
$encoding
),
$encoding
);
}
/**
* Enable Botan.io integration
*
* @param $token
* @param $custom
* @return Telegram
*/
public
function
enableBotan
(
$token
)
{
Botan
::
initializeBotan
(
$token
);
$this
->
botan_enabled
=
true
;
return
$this
;
}
}
structure.sql
View file @
771a5061
...
...
@@ -171,3 +171,14 @@ CREATE TABLE IF NOT EXISTS `conversation` (
FOREIGN
KEY
(
`chat_id`
)
REFERENCES
`chat`
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
COLLATE
=
utf8_general_ci
;
CREATE
TABLE
IF
NOT
EXISTS
`botan_shortener`
(
`id`
bigint
UNSIGNED
AUTO_INCREMENT
COMMENT
'Unique identifier for this entry.'
,
`user_id`
bigint
NULL
DEFAULT
NULL
COMMENT
'Unique user identifier'
,
`url`
text
NOT
NULL
DEFAULT
''
COMMENT
'Original URL'
,
`short_url`
CHAR
(
255
)
NOT
NULL
DEFAULT
''
COMMENT
'Shortened URL'
,
`created_at`
timestamp
NULL
DEFAULT
NULL
COMMENT
'Entry date creation'
,
PRIMARY
KEY
(
`id`
),
FOREIGN
KEY
(
`user_id`
)
REFERENCES
`user`
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
COLLATE
=
utf8_general_ci
;
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