-
-
Save fwartner/99d7cbb28af5452d629b1c8ea771b0d3 to your computer and use it in GitHub Desktop.
Using link buttons and deep linking for a seamless flow between your service and Slack with BotMan Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use BotMan\Drivers\Slack\SlackDriver; | |
Route::get('/', function () { | |
$task_id = uniqid(); | |
// Send the message to the channel | |
$result = app('botman')->say('example', '#botman-tests', SlackDriver::class, [ | |
'attachments' => json_encode([ | |
[ | |
'fallback' => 'Upgrade your Slack client to use messages like these.', | |
'color' => '#CC0000', | |
'actions' => [ | |
[ | |
'type' => 'button', | |
'text' => ':red_circle: Complete Task: '.$task_id, | |
'url' => url('/workflow/'.$task_id), | |
] | |
] | |
] | |
]) | |
]); | |
// Retrieve API result | |
$result = json_decode($result->getContent(), true); | |
// Store the task in the cache | |
$storedTasks = \Cache::get('tasks', []); | |
$storedTasks[$task_id] = [ | |
'channel' => $result['channel'], | |
'ts' => $result['message']['ts'], | |
]; | |
\Cache::put('tasks', $storedTasks, 60); | |
}); | |
Route::get('/workflow/{task_id}', function($task_id) { | |
return response('<form method="POST" action="/complete/'.$task_id.'"> | |
<input type="submit" value="Do The Thing" /> | |
</form>'); | |
}); | |
Route::post('/complete/{task_id}', function($task_id) { | |
$tasks = \Cache::get('tasks'); | |
$task = $tasks[$task_id]; | |
/** | |
* This is where your app's business logic would live. | |
* Once the task has been complete, the user will be directed to this `/complete` | |
* page, which shows a link back to their Slack conversation | |
*/ | |
/** | |
* When this page loads, we update the original Slack message to show that | |
* the pending task has been completed | |
*/ | |
$botman = app('botman'); | |
$botman->loadDriver(SlackDriver::class); | |
$botman->sendRequest('chat.update', [ | |
'channel' => $task['channel'], | |
'ts' => $task['ts'], | |
'text' => 'Task Complete!', | |
'attachments' => json_encode([ | |
[ | |
'fallback' => 'Upgrade your Slack client to use messages like these.', | |
'color' => '#36a64f', | |
'text' => ':white_check_mark: *Completed Task: '.$task_id.'*', | |
'mrkdwn_in' => ['text'] | |
] | |
]) | |
]); | |
// Get the message permalink to redirect the user back to Slack | |
$result = $botman->sendRequest('chat.getPermalink', [ | |
'channel' => $task['channel'], | |
'message_ts' => $task['ts'], | |
]); | |
$result = json_decode($result->getContent(), true); | |
return response('<a href="'.$result['permalink'].'">Return to Slack</a>'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment