php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.87k stars 951 forks source link

Cancel all active sessions #269

Closed khaninejad closed 7 years ago

khaninejad commented 8 years ago

What is the best way to cancel all users active session and send them to start commend after inactive time period? I know /cancel command but I want to do same thing to all users at once.

noplanman commented 8 years ago

What exactly do you mean by "active sessions"?

If you want to cancel all active conversations, you could create an admin command (or cron job) that set's all active conversations to stopped.

khaninejad commented 8 years ago

This is exactly what I need, do you have any sample?

On Wed, Aug 10, 2016 at 4:21 PM, Armando Lüscher notifications@github.com wrote:

What exactly do you mean by "active sessions"?

If you want to cancel all active conversations, you could create an admin command (or cron job) that set's all active conversations to stopped.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/akalongman/php-telegram-bot/issues/269#issuecomment-238843939, or mute the thread https://github.com/notifications/unsubscribe-auth/AEM1FB7mOohpXn733cWm1dzyeRb6-7Kgks5qebs-gaJpZM4JhBtm .

noplanman commented 8 years ago

For the conversations that are older than 30 days, the MySQL command would be like this:

UPDATE `conversation` SET `status` = 'stopped' WHERE `status` = 'active' AND `updated_at` < DATE_SUB(NOW(), INTERVAL 30 DAY)

But remember, this just stops ALL those active conversations! (Yes I know you know, just making sure 😉)

khaninejad commented 8 years ago

But it is not enough to cancel users current session! For example some user left bot in middle of conversation we need to expire their session and send them to start command or home command.

On Wed, Aug 10, 2016 at 4:56 PM, Armando Lüscher notifications@github.com wrote:

For the conversations that are older than 30 days, the MySQL command would be like this:

UPDATE conversation SET status = 'stopped' WHERE status = 'active' AND updated_at < DATE_SUB(NOW(), INTERVAL 30 DAY)

But remember, this just stops ALL those active conversations! (Yes I know you know, just making sure 😉)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/akalongman/php-telegram-bot/issues/269#issuecomment-238851201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEM1FHB6fGevXruox11fcgzwMHT4h_Paks5qecOSgaJpZM4JhBtm .

noplanman commented 8 years ago

Right, I'm not sure that's possible, as the users will still have the chat history with your bot, so they won't get the start message.

@jacklul @MBoretto Do you guys know maybe?

khaninejad commented 8 years ago

This is possible,check this bot: @HiwebOfficial_bot

On Wednesday, August 10, 2016, Armando Lüscher notifications@github.com wrote:

Right, I'm not sure that's possible, as the users will still have the chat history with your bot, so they won't get the start message.

@jacklul https://github.com/jacklul @MBoretto https://github.com/MBoretto Do you guys know maybe?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/akalongman/php-telegram-bot/issues/269#issuecomment-238871274, or mute the thread https://github.com/notifications/unsubscribe-auth/AEM1FHcyuI8yn-3xbXVoenbtTwuEOcgwks5qedY-gaJpZM4JhBtm .

jacklul commented 8 years ago

In my bots I'm cleaning conversation table from records older than 7 days. Also have failsafe code that hides keyboard when user messages bot but conversation is not in progress (in case they come back after 7+ days).

khaninejad commented 8 years ago

I think we need an expire time for conversation or commands.

On Wednesday, August 10, 2016, jacklul notifications@github.com wrote:

In my bots I'm cleaning conversation table from records older than 7 days.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/akalongman/php-telegram-bot/issues/269#issuecomment-238914985, or mute the thread https://github.com/notifications/unsubscribe-auth/AEM1FAnr9xnZTxCwHQQLiWlJYWtGDnCJks5qefYegaJpZM4JhBtm .

MBoretto commented 8 years ago

Timeouts mean cron. Otherwise you can check if a conversation has been expired the next time that that user interacts with the bot.

khaninejad commented 8 years ago

@MBoretto Do you have any general implementation for timeout?

khaninejad commented 8 years ago

This is my solution:

<?php
// Load composer
require __DIR__ . '/vendor/autoload.php';
require_once 'db.php';
require_once 'class.DBPDO.php';

use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Carbon\Carbon;
$API_KEY = 'xxxx';
$BOT_NAME = 'xxxx';
$telegram = new Telegram($API_KEY, $BOT_NAME);

$message = "Your session expired please touch /start to start again";
$update = [':status' => "active"];
$pdo = new DBPDO();

$res = $pdo->fetchAll("SELECT * FROM `conversation` WHERE `status` = 'active' AND `updated_at` < '".Carbon::parse('-15 minute')->toDateTimeString()."' ", $update);
foreach ($res as $user) {
  $chat_id = $user["chat_id"];
  $sql="UPDATE `conversation` SET `status` = 'stopped' WHERE `conversation`.`id` =".$user["id"];
  $pdo->execute($sql);
  if ($chat_id !== '' && $message !== '') {
      $data = [
          'chat_id' => $chat_id,
          'text'    => $message,
      ];
    $result = Request::sendMessage($data);
      if ($result->isOk()) {
          echo 'Message sent succesfully to: ' . $chat_id . "\n";
      } else {
          echo 'Sorry message not sent to: ' . $chat_id . "\n";
      }
  }
}
MBoretto commented 8 years ago

No... What would be nice to develop as I suggested here: https://github.com/akalongman/php-telegram-bot/issues/157 is to create something that performs operation like:

Is a sort of hack like you posted but inplemeted in the lib!

khaninejad commented 8 years ago

But I run this using cron job

MBoretto commented 7 years ago

Would be nice to add this to the wiki @khaninejad can you do this? would like to close

khaninejad commented 7 years ago

@MBoretto Done https://github.com/akalongman/php-telegram-bot/wiki/Cancel-all-active-sessions