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
e060ae01
Commit
e060ae01
authored
May 13, 2016
by
MBoretto
Browse files
Options
Browse Files
Download
Plain Diff
merge conflict
parents
2a96b072
e5b6d415
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
94 additions
and
47 deletions
+94
-47
README.md
README.md
+1
-1
DB.php
src/DB.php
+66
-43
Message.php
src/Entities/Message.php
+13
-0
Sticker.php
src/Entities/Sticker.php
+10
-2
Telegram.php
src/Telegram.php
+1
-1
structure.sql
structure.sql
+3
-0
No files found.
README.md
View file @
e060ae01
...
...
@@ -66,7 +66,7 @@ and have interactions in a matter of minutes.
The Bot can:
-
retrieve updates with webhook and getUpdate methods.
-
supports all types and methods according to Telegram API (
9 April
2016).
-
supports all types and methods according to Telegram API (
6 May
2016).
-
supports supergroups.
-
handle commands in chat with other bots.
-
manage Channel from the bot admin interface.
...
...
src/DB.php
View file @
e060ae01
...
...
@@ -364,6 +364,57 @@ class DB
}
}
/**
* Insert chat
*
* @todo Needs to return something if successful
*
* @param Entities\Chat $chat
* @param string $date
* @param int $migrate_to_chat_id
*/
public
static
function
insertChat
(
Chat
$chat
,
$date
,
$migrate_to_chat_id
=
null
)
{
if
(
!
self
::
isDbConnected
())
{
return
false
;
}
$chat_id
=
$chat
->
getId
();
$chat_title
=
$chat
->
getTitle
();
$type
=
$chat
->
getType
();
try
{
//chat table
$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
'
);
if
(
$migrate_to_chat_id
)
{
$type
=
'supergroup'
;
$sth2
->
bindParam
(
':id'
,
$migrate_to_chat_id
,
\PDO
::
PARAM_INT
);
$sth2
->
bindParam
(
':oldid'
,
$chat_id
,
\PDO
::
PARAM_INT
);
}
else
{
$sth2
->
bindParam
(
':id'
,
$chat_id
,
\PDO
::
PARAM_INT
);
$sth2
->
bindParam
(
':oldid'
,
$migrate_to_chat_id
,
\PDO
::
PARAM_INT
);
}
$sth2
->
bindParam
(
':type'
,
$type
,
\PDO
::
PARAM_INT
);
$sth2
->
bindParam
(
':title'
,
$chat_title
,
\PDO
::
PARAM_STR
,
255
);
$sth2
->
bindParam
(
':date'
,
$date
,
\PDO
::
PARAM_STR
);
$status
=
$sth2
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
}
/**
* Insert request into database
*
...
...
@@ -562,64 +613,35 @@ class DB
$date
=
self
::
getTimestamp
(
$message
->
getDate
());
$forward_from
=
$message
->
getForwardFrom
();
if
(
$forward_from
)
{
$forward_date
=
self
::
getTimestamp
(
$message
->
getForwardDate
());
}
$forward_from_chat
=
$message
->
getForwardFromChat
();
$photo
=
$message
->
getPhoto
();
$entities
=
$message
->
getEntities
();
$new_chat_member
=
$message
->
getNewChatMember
();
$new_chat_photo
=
$message
->
getNewChatPhoto
();
$left_chat_member
=
$message
->
getLeftChatMember
();
$migrate_from_chat_id
=
$message
->
getMigrateFromChatId
();
$migrate_to_chat_id
=
$message
->
getMigrateToChatId
();
try
{
//chat table
$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
'
);
$chat_title
=
$chat
->
getTitle
();
$type
=
$chat
->
getType
();
if
(
$migrate_to_chat_id
)
{
$type
=
'supergroup'
;
$sth2
->
bindParam
(
':id'
,
$migrate_to_chat_id
,
\PDO
::
PARAM_INT
);
$sth2
->
bindParam
(
':oldid'
,
$chat_id
,
\PDO
::
PARAM_INT
);
}
else
{
$sth2
->
bindParam
(
':id'
,
$chat_id
,
\PDO
::
PARAM_INT
);
$sth2
->
bindParam
(
':oldid'
,
$migrate_to_chat_id
,
\PDO
::
PARAM_INT
);
}
$sth2
->
bindParam
(
':type'
,
$type
,
\PDO
::
PARAM_INT
);
$sth2
->
bindParam
(
':title'
,
$chat_title
,
\PDO
::
PARAM_STR
,
255
);
$sth2
->
bindParam
(
':date'
,
$date
,
\PDO
::
PARAM_STR
);
$status
=
$sth2
->
execute
();
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
self
::
insertChat
(
$chat
,
$date
,
$migrate_to_chat_id
);
//Insert user and the relation with the chat
self
::
insertUser
(
$from
,
$date
,
$chat
);
//Forwarded object
if
(
$forward_from
||
$forward_from_chat
)
{
$forward_date
=
self
::
getTimestamp
(
$message
->
getForwardDate
());
}
//Insert the forwarded message user in users table
$forward_from
=
null
;
if
(
is_object
(
$forward_from
))
{
self
::
insertUser
(
$forward_from
,
$forward_date
);
$forward_from
=
$forward_from
->
getId
();
}
if
(
is_object
(
$forward_from_chat
))
{
self
::
insertChat
(
$forward_from_chat
,
$forward_date
);
$forward_from_chat
=
$forward_from_chat
->
getId
();
}
//New and left chat member
if
(
$new_chat_member
)
{
//Insert the new chat user
self
::
insertUser
(
$new_chat_member
,
$date
,
$chat
);
...
...
@@ -634,7 +656,7 @@ class DB
//message Table
$sth
=
self
::
$pdo
->
prepare
(
'INSERT IGNORE INTO `'
.
TB_MESSAGE
.
'`
(
`id`, `user_id`, `date`, `chat_id`, `forward_from`,
`id`, `user_id`, `date`, `chat_id`, `forward_from`,
`forward_from_chat`,
`forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`,
`location`, `venue`, `new_chat_member`, `left_chat_member`,
...
...
@@ -643,7 +665,7 @@ class DB
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
)
VALUES (
:message_id, :user_id, :date, :chat_id, :forward_from,
:message_id, :user_id, :date, :chat_id, :forward_from,
:forward_from_chat,
:forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
:photo, :sticker, :video, :voice, :caption, :contact,
:location, :venue, :new_chat_member, :left_chat_member,
...
...
@@ -688,6 +710,7 @@ class DB
$sth
->
bindParam
(
':user_id'
,
$from_id
,
\PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':date'
,
$date
,
\PDO
::
PARAM_STR
);
$sth
->
bindParam
(
':forward_from'
,
$forward_from
,
\PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':forward_from_chat'
,
$forward_from_chat
,
\PDO
::
PARAM_INT
);
$sth
->
bindParam
(
':forward_date'
,
$forward_date
,
\PDO
::
PARAM_STR
);
$reply_chat_id
=
null
;
if
(
$reply_to_message_id
)
{
...
...
src/Entities/Message.php
View file @
e060ae01
...
...
@@ -24,6 +24,8 @@ class Message extends Entity
protected
$forward_from
;
protected
$forward_from_chat
;
protected
$forward_date
;
protected
$reply_to_message
;
...
...
@@ -120,6 +122,12 @@ class Message extends Entity
$this
->
forward_from
=
isset
(
$data
[
'forward_from'
])
?
$data
[
'forward_from'
]
:
null
;
if
(
!
empty
(
$this
->
forward_from
))
{
$this
->
forward_from
=
new
User
(
$this
->
forward_from
);
}
$this
->
forward_from_chat
=
isset
(
$data
[
'forward_from_chat'
])
?
$data
[
'forward_from_chat'
]
:
null
;
if
(
!
empty
(
$this
->
forward_from_chat
))
{
$this
->
forward_from_chat
=
new
Chat
(
$this
->
forward_from_chat
);
}
$this
->
forward_date
=
isset
(
$data
[
'forward_date'
])
?
$data
[
'forward_date'
]
:
null
;
...
...
@@ -343,6 +351,11 @@ class Message extends Entity
return
$this
->
forward_from
;
}
public
function
getForwardFromChat
()
{
return
$this
->
forward_from_chat
;
}
public
function
getForwardDate
()
{
return
$this
->
forward_date
;
...
...
src/Entities/Sticker.php
View file @
e060ae01
...
...
@@ -18,9 +18,9 @@ class Sticker extends Entity
protected
$width
;
protected
$height
;
protected
$thumb
;
protected
$emoji
;
protected
$file_size
;
public
function
__construct
(
array
$data
)
{
...
...
@@ -45,8 +45,9 @@ class Sticker extends Entity
}
$this
->
thumb
=
new
PhotoSize
(
$this
->
thumb
);
$this
->
file_size
=
isset
(
$data
[
'file_size'
])
?
$data
[
'file_size
'
]
:
null
;
$this
->
emoji
=
isset
(
$data
[
'emoji'
])
?
$data
[
'emoji
'
]
:
null
;
$this
->
file_size
=
isset
(
$data
[
'file_size'
])
?
$data
[
'file_size'
]
:
null
;
}
public
function
getFileId
()
...
...
@@ -63,10 +64,17 @@ class Sticker extends Entity
{
return
$this
->
height
;
}
public
function
getThumb
()
{
return
$this
->
thumb
;
}
public
function
getEmoji
()
{
return
$this
->
emoji
;
}
public
function
getFileSize
()
{
return
$this
->
file_size
;
...
...
src/Telegram.php
View file @
e060ae01
...
...
@@ -30,7 +30,7 @@ class Telegram
*
* @var string
*/
protected
$version
=
'0.3
1
.0'
;
protected
$version
=
'0.3
2
.0'
;
/**
* Telegram API key
...
...
structure.sql
View file @
e060ae01
...
...
@@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS `message` (
`user_id`
bigint
NULL
COMMENT
'User identifier'
,
`date`
timestamp
NULL
DEFAULT
NULL
COMMENT
'Date the message was sent in timestamp format'
,
`forward_from`
bigint
NULL
DEFAULT
NULL
COMMENT
'User id. For forwarded messages, sender of the original message'
,
`forward_from_chat`
bigint
NULL
DEFAULT
NULL
COMMENT
'Chat id. For forwarded messages from channel'
,
`forward_date`
timestamp
NULL
DEFAULT
NULL
COMMENT
'For forwarded messages, date the original message was sent in Unix time'
,
`reply_to_chat`
bigint
NULL
DEFAULT
NULL
COMMENT
'Chat identifier.'
,
`reply_to_message`
bigint
UNSIGNED
DEFAULT
NULL
COMMENT
'Message is a reply to another message.'
,
...
...
@@ -111,6 +112,7 @@ CREATE TABLE IF NOT EXISTS `message` (
PRIMARY
KEY
(
`chat_id`
,
`id`
),
KEY
`user_id`
(
`user_id`
),
KEY
`forward_from`
(
`forward_from`
),
KEY
`forward_from_chat`
(
`forward_from_chat`
),
KEY
`reply_to_chat`
(
`reply_to_chat`
),
KEY
`reply_to_message`
(
`reply_to_message`
),
KEY
`new_chat_member`
(
`new_chat_member`
),
...
...
@@ -121,6 +123,7 @@ CREATE TABLE IF NOT EXISTS `message` (
FOREIGN
KEY
(
`user_id`
)
REFERENCES
`user`
(
`id`
),
FOREIGN
KEY
(
`chat_id`
)
REFERENCES
`chat`
(
`id`
),
FOREIGN
KEY
(
`forward_from`
)
REFERENCES
`user`
(
`id`
),
FOREIGN
KEY
(
`forward_from_chat`
)
REFERENCES
`chat`
(
`id`
),
FOREIGN
KEY
(
`reply_to_chat`
,
`reply_to_message`
)
REFERENCES
`message`
(
`chat_id`
,
`id`
),
FOREIGN
KEY
(
`forward_from`
)
REFERENCES
`user`
(
`id`
),
FOREIGN
KEY
(
`new_chat_member`
)
REFERENCES
`user`
(
`id`
),
...
...
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