Skip to content

Commit

Permalink
Send weather forecast to someone
Browse files Browse the repository at this point in the history
  • Loading branch information
FRAMGIA\nguyen.van.minhb committed Apr 4, 2019
1 parent 4819996 commit b2dc6b1
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.idea
95 changes: 95 additions & 0 deletions app/Console/Commands/SendLoveMessage.php
@@ -0,0 +1,95 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use wataridori\ChatworkSDK\ChatworkSDK;
use wataridori\ChatworkSDK\ChatworkRoom;

class SendLoveMessage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:love-msg';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send love message to recive nothing!';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$weatherInfor = $this->getWeatherForeCastInformationJSON();

ChatworkSDK::setApiKey(config('services.chatwork.api_key'));
$room = new ChatworkRoom(config('services.chatwork.room_id'));

$room->sendMessageToAll($this->makeLoveMsg($weatherInfor));
}

public function getWeatherForeCastInformationJSON()
{
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://dataservice.accuweather.com/forecasts/v1/daily/1day/353412?apikey=kDJ6a6fH9jAPQUUeUSiqoSbA0Yf78AzN&language=vi&metric=true");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$weatherInfor = json_decode(curl_exec($ch));

// close curl resource to free up system resources
curl_close($ch);

return $weatherInfor;
}

public function makeLoveMsg($weatherInfor)
{
$loveMsg = "Dự báo thời tiết hôm nay có thể nắng, có thể mưa mà cũng có thể không nắng không mưa!!";

if (property_exists($weatherInfor, 'Headline') && (property_exists($weatherInfor, 'DailyForecasts'))) {
$header = $weatherInfor->Headline;
$forecast = $weatherInfor->DailyForecasts;

$text = $header->Text;
$minTemperature = $forecast[0]->Temperature->Minimum->Value;
$maxTemperature = $forecast[0]->Temperature->Maximum->Value;
$dayWeather = $forecast[0]->Day->IconPhrase;
$nightWeather = $forecast[0]->Night->IconPhrase;

$loveMsg = <<<LOVEMSG
Thời tiết chung: {$text}
Nhiệt độ thấp nhất: {$minTemperature}
Nhiệt độ cao nhất: {$maxTemperature}
Nhiệt độ trong ngày: {$minTemperature} - {$maxTemperature}
Thời tiết ban ngày: {$dayWeather}
Thời tiết buổi tối: {$nightWeather}
LOVEMSG;
}

return $loveMsg;
}
}
7 changes: 4 additions & 3 deletions app/Console/Kernel.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Console\Commands\SendLoveMessage;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
SendLoveMessage::class,
];

/**
Expand All @@ -24,8 +25,8 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('send:love-msg')
->dailyAt("05:50");
}

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -11,7 +11,8 @@
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0"
"laravel/tinker": "^1.0",
"wataridori/chatwork-sdk": "^0.3.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
Expand Down
1 change: 1 addition & 0 deletions config/app.php
Expand Up @@ -225,6 +225,7 @@
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'ChatworkSDK' => \wataridori\ChatworkSDK\ChatworkSDK::class,

],

Expand Down
4 changes: 4 additions & 0 deletions config/services.php
Expand Up @@ -44,4 +44,8 @@
],
],

'chatwork' => [
'api_key' => env('CHATWORK_API_KEY'),
'room_id' => env('CHATWORK_ROOMID'),
],
];

0 comments on commit b2dc6b1

Please sign in to comment.