Commit a6f664f5 authored by MBoretto's avatar MBoretto

Introducing Super groups

parent 56bf8fc8
......@@ -21,7 +21,8 @@ Bot aims to provide a platform where one could simply write a plugin
and have interactions in a matter of minutes.
The Bot can:
- retrive update with webhook and getUpdate methods.
- supports all types and methods according to Telegram API (2015 October 28).
- supports all types and methods according to Telegram API (2015 November).
- supports supergroups.
- handle commands in chat with other bots.
- manage Channel from the bot admin interface (**new!**)
......@@ -206,10 +207,10 @@ then run
./getUpdateCLI.php
```
### Types
All types implemented according to Telegram API (2015 October 28).
All types implemented according to Telegram API (2015 November).
### Methods
All methods implemented according to Telegram API (2015 October 28).
All methods implemented according to Telegram API (2015 November).
####Send Photo
To send a local photo provide the file path as second param:
......@@ -282,11 +283,13 @@ All methods implemented can be used to manage channels.
The bot is able to recognise commands in chat with multiple bot (/command@mybot ).
It can execute command triggering a chat event. Here's the list:
- Group chat created (**GroupchatcreatedCommand.php**)
- New chat participant (**NewchatparticipantCommand.php**)
- Delete chat photo (**DeletechatphotoCommand.php**)
- New chat title (**NewchattitleCommand.php**)
- Left chat participant (**LeftchatparticipantCommand.php**)
- New chat title (**NewchattitleCommand.php**)
- Delete chat photo (**DeletechatphotoCommand.php**)
- Group chat created (**GroupchatcreatedCommand.php**)
- Super group chat created (**SupergroupchatcreatedCommand.php**)
- Channel chat created (**ChannelchatcreatedCommand.php**)
**GenericCommand.php** let you handle commands that don't exist or to
use commands as a variable:
......
......@@ -49,7 +49,8 @@ class ChatsCommand extends Command
$text = $message->getText(true);
$results = DB::selectChats(
true, //Send to chats (group chat)
true, //Send to groups (group chat)
true, //Send to supergroups (single chat)
true, //Send to users (single chat)
null, //'yyyy-mm-dd hh:mm:ss' date range from
null //'yyyy-mm-dd hh:mm:ss' date range to
......@@ -57,6 +58,7 @@ class ChatsCommand extends Command
$user_chats = 0;
$group_chats = 0;
$super_group_chats = 0;
$text = "List of bot chats:\n";
foreach ($results as $result) {
......@@ -66,20 +68,24 @@ class ChatsCommand extends Command
$chat = new Chat($result);
if ($chat->isPrivateChat()) {
$text .= '- U '.$this->tryMentionChat($chat)."\n";
$text .= '- P '.$this->tryMentionChat($chat)."\n";
++$user_chats;
} else {
} elseif ($chat->isGroupChat()) {
$text .= '- G '.$chat->getTitle()."\n";
++$group_chats;
} elseif ($chat->isSuperGroup()) {
$text .= '- S '.$chat->getTitle()."\n";
++$super_group_chats;
}
}
if (($group_chats + $user_chats) == 0) {
if (($group_chats + $user_chats + $super_group_chats) == 0) {
$text = "No chats found..";
} else {
$text .= "\nUser Chats: ".$user_chats;
$text .= "\nGroup Chats: ".$group_chats;
$text .= "\nTot: ".($group_chats + $user_chats);
$text .= "\nPrivate Chats: ".$user_chats;
$text .= "\nGroup: ".$group_chats;
$text .= "\nSuper Group: ".$super_group_chats;
$text .= "\nTot: ".($group_chats + $user_chats + $super_group_chats);
}
$data = [];
......
......@@ -54,7 +54,8 @@ class SendtoallCommand extends Command
$results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
array('text'=> $text), //Param to evaluate the request
true, //Send to chats (group chat)
true, //Send to groups (group chat)
true, //Send to super groups chats (super group chat)
true, //Send to users (single chat)
null, //'yyyy-mm-dd hh:mm:ss' date range from
null //'yyyy-mm-dd hh:mm:ss' date range to
......
<?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;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class ChannelchatcreatedCommand extends Command
{
protected $name = 'Channelchatcreated';
protected $description = 'Channel chat created';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$channel_chat_created = $message->getChannelChatCreated();
// temporary do nothing
}
}
......@@ -31,7 +31,7 @@ class EchoCommand extends Command
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
$data = array();
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = $text;
......
<?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;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class SupergroupchatcreatedCommand extends Command
{
protected $name = 'Supergroupchatcreated';
protected $description = 'Super group chat created';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$text = '';
if ($message->getSuperGroupChatCreated()) {
$text = "Your group has become a Supergroup!\n";
$text .= 'Chat id has changed from'.$message->getMigrateFromChatId().' to '.$message->getMigrateToChatId();
}
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = $text;
$result = Request::sendMessage($data);
return $result;
}
}
......@@ -239,11 +239,16 @@ class DB
$new_chat_photo = $message->getNewChatPhoto();
$left_chat_participant = $message->getLeftChatParticipant();
$migrate_from_chat_id = $message->getMigrateFromChatId();
if (is_null($migrate_from_chat_id)) {
$migrate_from_chat_id = 0;
}
try {
//chats table
$sth2 = self::$pdo->prepare('INSERT INTO `'.TB_CHATS.'`
(`id`, `type`, `title`, `created_at` ,`updated_at`)
VALUES (:id, :type, :title, :date, :date)
(`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`)
VALUES (:id, :type, :title, :date, :date, :oldid)
ON DUPLICATE KEY UPDATE `title`=:title, `updated_at`=:date');
$chat_title = $chat->getTitle();
......@@ -253,6 +258,7 @@ class DB
$sth2->bindParam(':type', $type, \PDO::PARAM_INT);
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);
$sth2->bindParam(':oldid', $migrate_from_chat_id, \PDO::PARAM_INT);
$status = $sth2->execute();
......@@ -297,13 +303,17 @@ class DB
`forward_date`, `reply_to_message`, `text`, `audio`, `document`,
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`,
`location`, `new_chat_participant`, `left_chat_participant`,
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
`supergroup_chat_created`, `channel_chat_created`,
`migrate_from_chat_id`, `migrate_to_chat_id`
)
VALUES (:update_id, :message_id, :user_id, :date, :chat_id, :forward_from,
:forward_date, :reply_to_message, :text, :audio, :document,
:photo, :sticker, :video, :voice, :caption, :contact,
:location, :new_chat_participant, :left_chat_participant,
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
:supergroup_chat_created, :channel_chat_created,
:migrate_from_chat_id, :migrate_to_chat_id
)');
$update_id = $update->getUpdateId();
......@@ -322,6 +332,9 @@ class DB
$new_chat_title = $message->getNewChatTitle();
$delete_chat_photo = $message->getDeleteChatPhoto();
$group_chat_created = $message->getGroupChatCreated();
$supergroup_chat_created = $message->getSupergroupChatCreated();
$channel_chat_created = $message->getChannelChatCreated();
$migrate_from_chat_id = $message->getMigrateFromChatId();
$sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT);
$sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
......@@ -372,7 +385,10 @@ class DB
$sth->bindParam(':new_chat_photo', $new_chat_photo, \PDO::PARAM_STR);
$sth->bindParam(':delete_chat_photo', $delete_chat_photo, \PDO::PARAM_STR);
$sth->bindParam(':group_chat_created', $group_chat_created, \PDO::PARAM_STR);
$sth->bindParam(':supergroup_chat_created', $migrate_from_chat_id, \PDO::PARAM_INT);
$sth->bindParam(':channel_chat_created', $supergroup_chat_created, \PDO::PARAM_INT);
$sth->bindParam(':migrate_from_chat_id', $channel_chat_created, \PDO::PARAM_INT);
$sth->bindParam(':migrate_to_chat_id', $migrate_from_chat_id, \PDO::PARAM_INT);
$status = $sth->execute();
} catch (PDOException $e) {
......@@ -391,7 +407,8 @@ class DB
* @return array selected rows
*/
public static function selectChats(
$select_chats = true,
$select_groups = true,
$select_super_groups = true,
$select_users = true,
$date_from = null,
$date_to = null
......@@ -400,7 +417,7 @@ class DB
return false;
}
if (!$select_chats & !$select_users) {
if (!$select_groups & !$select_users & !$select_super_groups) {
return false;
}
try {
......@@ -415,10 +432,17 @@ class DB
$chat_or_user = '';
$where = [];
$tokens = [];
if ($select_chats & !$select_users) {
$where[] = TB_CHATS.'.`id` < 0';
} elseif (!$select_chats & $select_users) {
$where[] = TB_CHATS.'.`id` > 0';
if (!$select_groups || !$select_users || !$select_super_groups) {
if ($select_groups) {
$where[] = TB_CHATS.'.`type` = "group"';
}
if ($select_super_groups) {
$where[] = TB_CHATS.'.`type` = "supergroup"';
}
if ($select_users) {
$where[] = TB_CHATS.'.`type` = "private"';
}
}
if (! is_null($date_from)) {
......
......@@ -60,6 +60,16 @@ class Message extends Entity
protected $delete_chat_photo;
protected $group_chat_created;
//TODO new items
protected $supergroup_chat_created;
protected $channel_chat_created;
protected $migrate_to_chat_id;
protected $migrate_from_chat_id;
private $command;
......@@ -177,7 +187,7 @@ class Message extends Entity
}
$this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null;
if ($this->new_chat_title) {
if (!is_null($this->new_chat_title)) {
$this->type = 'new_chat_title';
}
......@@ -201,6 +211,19 @@ class Message extends Entity
$this->type = 'group_chat_created';
}
$this->supergroup_chat_created = isset($data['supergroup_chat_created']) ? $data['supergroup_chat_created'] : null;
if ($this->supergroup_chat_created) {
$this->type = 'supergroup_chat_created';
}
$this->channel_chat_created = isset($data['channel_chat_created']) ? $data['channel_chat_created'] : null;
if ($this->channel_chat_created) {
$this->type = 'channel_chat_created';
}
$this->migrate_to_chat_id = isset($data['migrate_to_chat_id']) ? $data['migrate_to_chat_id'] : null;
$this->migrate_from_chat_id = isset($data['migrate_from_chat_id']) ? $data['migrate_from_chat_id'] : null;
}
//return the entire command like /echo or /echo@bot1 if specified
......@@ -373,6 +396,25 @@ class Message extends Entity
return $this->group_chat_created;
}
public function getSupergroupChatCreated()
{
return $this->supergroup_chat_created;
}
public function getChannelChatCreated()
{
return $this->channel_chat_created;
}
public function getMigrateToChatId()
{
return $this->migrate_to_chat_id;
}
public function getMigrateFromChatId()
{
return $this->migrate_from_chat_id;
}
public function botAddedInChat()
{
......
......@@ -424,7 +424,8 @@ class Request
public static function sendToActiveChats(
$callback_function,
array $data,
$send_chats = true,
$send_groups = true,
$send_super_groups = true,
$send_users = true,
$date_from = null,
$date_to = null
......@@ -435,7 +436,7 @@ class Request
throw new TelegramException('Methods: '.$callback_function.' not found in class Request.');
}
$chats = DB::selectChats($send_chats, $send_users, $date_from, $date_to);
$chats = DB::selectChats($send_groups, $send_super_groups, $send_users, $date_from, $date_to);
$results = [];
foreach ($chats as $row) {
......
......@@ -29,7 +29,7 @@ class Telegram
*
* @var string
*/
protected $version = '0.24.0';
protected $version = '0.25.0';
/**
* Telegram API key
......@@ -57,7 +57,7 @@ class Telegram
*
* @var array
*/
protected $commands_dir = array();
protected $commands_dir = [];
/**
* Update object
......@@ -449,6 +449,14 @@ class Telegram
// trigger group_chat_created
return $this->executeCommand('Groupchatcreated', $update);
break;
case 'supergroup_chat_created':
// trigger super_group_chat_created
return $this->executeCommand('Supergroupchatcreated', $update);
break;
case 'channel_chat_created':
// trigger channel_chat_created
return $this->executeCommand('Channelchatcreated', $update);
break;
}
}
......
......@@ -23,6 +23,10 @@ CREATE TABLE `messages` (
`new_chat_photo` TEXT DEFAULT '' COMMENT 'Array of PhotoSize objects. A group photo was change to this value',
`delete_chat_photo` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group photo was deleted',
`group_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group has been created',
`supergroup_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the supergroup has been created',
`channel_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the channel chat has been created',
`migrate_from_chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Migrate from chat identifier.',
`migrate_to_chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Migrate to chat identifier.',
PRIMARY KEY (`update_id`),
KEY `message_id` (`message_id`),
KEY `user_id` (`user_id`)
......@@ -41,10 +45,11 @@ CREATE TABLE `users` (
CREATE TABLE `chats` (
`id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user or chat identifier',
`type` CHAR(10) DEFAULT '' COMMENT 'chat type private group, supergroup or channel',
`type` CHAR(10) DEFAULT '' COMMENT 'chat type private, group, supergroup or channel',
`title` CHAR(255) DEFAULT '' COMMENT 'chat title null if case of single chat with the bot',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date creation',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date update',
`old_id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique chat identifieri this is filled when a chat is converted to a superchat',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment