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
546d62ac
Commit
546d62ac
authored
Feb 16, 2016
by
MBoretto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
introducing db external connection
parent
aae0d911
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
29 deletions
+81
-29
README.md
README.md
+6
-0
DB.php
src/DB.php
+61
-27
Telegram.php
src/Telegram.php
+14
-2
No files found.
README.md
View file @
546d62ac
...
...
@@ -344,7 +344,13 @@ $telegram->enableMySQL($mysql_credentials, $BOT_NAME . '_');
Consider to use the
*utf8mb4*
branch if you find some special characters problems.
You can also store inline query and chosen inline query in the database.
#### External Database connection
Is possible to provide to the library an external mysql connection. Here's how to configure it:
```
php
$telegram
->
enableExternalMySQL
(
$external_pdo_connection
)
//$telegram->enableExternalMySQL($external_pdo_connection, $table_prefix)
```
### Channels Support
All methods implemented can be used to manage channels.
...
...
src/DB.php
View file @
546d62ac
...
...
@@ -74,47 +74,81 @@ class DB
throw
new
TelegramException
(
'MySQL credentials not provided!'
);
}
self
::
$telegram
=
$telegram
;
self
::
$mysql_credentials
=
$credentials
;
self
::
$table_prefix
=
$table_prefix
;
$dsn
=
'mysql:host='
.
$credentials
[
'host'
]
.
';dbname='
.
$credentials
[
'database'
];
$options
=
[
\PDO
::
MYSQL_ATTR_INIT_COMMAND
=>
'SET NAMES utf8'
];
try
{
$pdo
=
new
\PDO
(
$dsn
,
$credentials
[
'user'
],
$credentials
[
'password'
],
$options
);
$pdo
->
setAttribute
(
\PDO
::
ATTR_ERRMODE
,
\PDO
::
ERRMODE_WARNING
);
}
catch
(
\PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
//Define table
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_INLINE_QUERY'
))
{
define
(
'TB_INLINE_QUERY'
,
self
::
$table_prefix
.
'inline_query'
);
}
self
::
$pdo
=
$pdo
;
self
::
$telegram
=
$telegram
;
self
::
$mysql_credentials
=
$credentials
;
self
::
$table_prefix
=
$table_prefix
;
if
(
!
defined
(
'TB_CHOSEN_INLINE_QUERY'
))
{
define
(
'TB_CHOSEN_INLINE_QUERY'
,
self
::
$table_prefix
.
'chosen_inline_query'
);
}
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'
);
}
self
::
defineTable
();
}
catch
(
\PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
return
self
::
$pdo
;
}
/**
* External Initialize
*
* Let you use the class with an external already existing Pdo Mysql connection.
*
* @param PDO $external_pdo_connection PDO database object
* @param Telegram $telegram Telegram object to connect with this object
* @param string $table_prefix Table prefix
*
* @return PDO PDO database object
*/
public
static
function
externalInitialize
(
$external_pdo_connection
,
Telegram
$telegram
,
$table_prefix
=
null
)
{
if
(
empty
(
$external_pdo_connection
))
{
throw
new
TelegramException
(
'MySQL external connection not provided!'
);
}
self
::
$pdo
=
$pdo
;
self
::
$telegram
=
$telegram
;
self
::
$mysql_credentials
=
null
;
self
::
$table_prefix
=
$table_prefix
;
self
::
defineTable
();
return
self
::
$pdo
;
}
/**
* Define all the table with the proper prefix
*/
protected
static
function
defineTable
()
{
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_INLINE_QUERY'
))
{
define
(
'TB_INLINE_QUERY'
,
self
::
$table_prefix
.
'inline_query'
);
}
if
(
!
defined
(
'TB_CHOSEN_INLINE_QUERY'
))
{
define
(
'TB_CHOSEN_INLINE_QUERY'
,
self
::
$table_prefix
.
'chosen_inline_query'
);
}
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'
);
}
}
/**
* Check if database connection has been created
*
...
...
src/Telegram.php
View file @
546d62ac
...
...
@@ -176,9 +176,9 @@ class Telegram
}
/**
* Initialize
* Initialize
Database connection
*
* @param array $credential
* @param array
$credential
* @param string $table_prefix
*/
public
function
enableMySQL
(
array
$credential
,
$table_prefix
=
null
)
...
...
@@ -187,6 +187,18 @@ class Telegram
$this
->
mysql_enabled
=
true
;
}
/**
* Initialize Database external connection
*
* @param PDO $external_pdo_connection PDO database object
* @param string $table_prefix
*/
public
function
enableExternalMySQL
(
$external_pdo_connection
,
$table_prefix
=
null
)
{
$this
->
pdo
=
DB
::
externalInitialize
(
$external_pdo_connection
,
$this
,
$table_prefix
);
$this
->
mysql_enabled
=
true
;
}
/**
* Get commands list
*
...
...
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