From 79d17bb890f85ea8ff7123d8fc0d8429a9bc7b44 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 13 Aug 2018 20:52:17 -0700 Subject: [PATCH 1/3] improve sponsor method --- app/Http/Controllers/BotManController.php | 38 +++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/BotManController.php b/app/Http/Controllers/BotManController.php index 095406e..320010a 100644 --- a/app/Http/Controllers/BotManController.php +++ b/app/Http/Controllers/BotManController.php @@ -142,29 +142,35 @@ public function speaker_schedule(Botman $bot) public function sponsor_information(Botman $bot) { + // get the sponsor the user is asking about $sponsor = data_get($bot->getMessage()->getExtras(), 'apiParameters.sponsor'); - $url = "https://wavephp-conf.firebaseio.com/sponsors/{$sponsor}.json"; - - $results = json_decode(file_get_contents($url), true); - - $name = $results['name']; - $description = $results['description']; - $twitter = $results['twitter']; - $url = $results['url']; - $logo = $results['logo']; + // fetch the sponsor details from Firebase + $sponsorData = json_decode( + file_get_contents("https://wavephp-conf.firebaseio.com/sponsors/{$sponsor}.json"), + true + ); + + // generate formatted response text + $formattedResponse = vsprintf("%s\n%s\n%s", [ + $sponsorData['description'], + $sponsorData['url'], + 'https://twitter.com/' . $sponsorData['twitter'], + ]); - $attachment = new Image($logo, [ + // create attachment with sponsor logo image + $attachment = new Image($sponsorData['logo'], [ 'custom_payload' => true, ]); - $message = OutgoingMessage::create()->withAttachment($attachment); + // build the logo message + $message = OutgoingMessage::create() + ->withAttachment($attachment); + + // send the logo $bot->reply($message); - $bot->reply(vsprintf("%s: %s\n%s", [ - $name, - $description, - $url, - ])); + // send text + $bot->reply($formattedResponse); } } From 84922260d06f63ada5b4b338ebdb17880ada6621 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 13 Aug 2018 21:19:36 -0700 Subject: [PATCH 2/3] improve speaker method --- app/Http/Controllers/BotManController.php | 28 +++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/BotManController.php b/app/Http/Controllers/BotManController.php index 320010a..552f78e 100644 --- a/app/Http/Controllers/BotManController.php +++ b/app/Http/Controllers/BotManController.php @@ -84,27 +84,31 @@ public function speaker(Botman $bot) public function speaker_bio(Botman $bot) { + // get the id of the speaker the user is asking about $speaker = data_get($bot->getMessage()->getExtras(), 'apiParameters.speaker'); - $url = "https://wavephp-conf.firebaseio.com/speakers/{$speaker}.json"; - - $results = json_decode(file_get_contents($url), true); - - $bio = $results['bio']; - $photo_url = $results['photo']; - - $bot->reply($bio); + // fetch the speaker details + $speakerData = json_decode( + file_get_contents("https://wavephp-conf.firebaseio.com/speakers/{$speaker}.json"), + true + ); - // Create attachment - $attachment = new Image($photo_url, [ + // create attachment with speaker image + $attachment = new Image($speakerData['photo'], [ 'custom_payload' => true, ]); - // Build message object + // build the speaker image message $message = OutgoingMessage::create()->withAttachment($attachment); - // Reply message object + // Reply with speaker image $bot->reply($message); + + // reply with the speaker's biography and twitter + $result = $bot->reply(vsprintf("%s\n%s", [ + $speakerData['bio'], + 'https://twitter.com/' . $speakerData['twitter'], + ])); } public function speaker_schedule(Botman $bot) From 63b6d380c66c94afc6dd5f1e6f0442a1f81ccb07 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 13 Aug 2018 21:38:13 -0700 Subject: [PATCH 3/3] improve presentation method --- app/Http/Controllers/BotManController.php | 38 +++++++++++++++-------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/BotManController.php b/app/Http/Controllers/BotManController.php index 552f78e..662b8bf 100644 --- a/app/Http/Controllers/BotManController.php +++ b/app/Http/Controllers/BotManController.php @@ -105,7 +105,7 @@ public function speaker_bio(Botman $bot) $bot->reply($message); // reply with the speaker's biography and twitter - $result = $bot->reply(vsprintf("%s\n%s", [ + $bot->reply(vsprintf("%s\n%s", [ $speakerData['bio'], 'https://twitter.com/' . $speakerData['twitter'], ])); @@ -113,10 +113,19 @@ public function speaker_bio(Botman $bot) public function speaker_schedule(Botman $bot) { + // get the id of the speaker the user is asking about $speakerId = data_get($bot->getMessage()->getExtras(), 'apiParameters.speaker'); + // fetch the speaker details + $speakerData = json_decode( + file_get_contents("https://wavephp-conf.firebaseio.com/speakers/{$speakerId}.json"), + true + ); + + // get the schedule for WavePHP $schedule = json_decode(file_get_contents('https://wavephp-conf.firebaseio.com/schedule.json'), true); + // map the speaker's presentations to a nice format $speakersPresentations = collect($schedule) // filter out the talks that do not belong to the given speaker ->filter(function ($presentation) use ($speakerId){ @@ -128,20 +137,23 @@ public function speaker_schedule(Botman $bot) }) // map to a nice sentence ->map(function ($presentation){ - $title = $presentation['talk']['title']; - $start = $presentation['start']; - $location = $presentation['location']; - return vsprintf('%s in %s at %s on %s', [ - $title, - $location, - Carbon::parse($start)->format('g:ia'), - Carbon::parse($start)->format('l'), + $presentation['talk']['title'], + $presentation['location'], + Carbon::parse($presentation['start'])->format('g:ia'), + Carbon::parse($presentation['start'])->format('l'), ]); - }) - ->implode("\n"); - - $bot->reply($speakersPresentations); + }); + + // format the response + $responseText = vsprintf("%s's %s:\n%s", [ + $speakerData['name'], + str_plural('presentation', $speakersPresentations->count()), + $speakersPresentations->implode("\n"), + ]); + + // send reply + $bot->reply($responseText); } public function sponsor_information(Botman $bot)