Skip to content

Commit

Permalink
Adds scheduled quotes to TifuBot (fixes #11, fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarmlage committed May 1, 2020
1 parent 353774d commit dab8f6d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
78 changes: 78 additions & 0 deletions app/Console/Commands/sendQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Telegram\Bot\Api as Telegram;

use \App\Models\Category;
use \App\Models\Quote;
use \App\Models\TelegramCanal;


class sendQuote extends Command
{
protected $telegram;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'telegram:sendquote {canal?} {tag?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send random quote to a channel (tag is optional)';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
//$this->telegram = new Api(env('TELEGRAM_BOT_TOKEN'));
$this->telegram = new Telegram(
env('TELEGRAM_BOT_TOKEN')
);
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$canal = $this->argument('canal');
$tag = $this->argument('tag');

$canales = TelegramCanal::where('active', 1)->get();
if($canal) {
$canales = TelegramCanal::where('name', "#".$canal)->where('active', 1)->get();
}

foreach($canales as $canal){
$this->chat_id = $canal->chat_id;
$quote = Quote::where('chat_id', $this->chat_id)->where('active', 1)->orderByRaw("RAND()")->limit(1)->first();
if($tag) {
$tag = Category::where('slug', $tag)->first();
$quote = $tag->quotes()->where('chat_id', $this->chat_id)->where('active', 1)->orderByRaw("RAND()")->limit(1)->first();
}

$data = [
'chat_id' => $this->chat_id,
'parse_mode' => 'HTML',
'text' => $quote->quote,
];
$this->telegram->sendMessage($data);
}

}
}

11 changes: 9 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('telegram:sendquote BOFHers buenosdias')
->weekdays()
->timezone('Europe/Madrid')
->dailyAt('8:30');

$schedule->command('telegram:sendquote')
->hourlyAt(30)
->timezone('Europe/Madrid')
->between('9:00', '23:00');
}

/**
Expand Down

0 comments on commit dab8f6d

Please sign in to comment.