Skip to content

Instantly share code, notes, and snippets.

@mofman
Created February 5, 2018 12:31
Show Gist options
  • Save mofman/c064c73a532ddf5e065d3952d363a428 to your computer and use it in GitHub Desktop.
Save mofman/c064c73a532ddf5e065d3952d363a428 to your computer and use it in GitHub Desktop.
Botman Location not responding
<?php
namespace xxxxxxxx\deepriverrock\controllers;
use xxxxxxxx\deepriverrock\DeepRiverrock;
use Yii;
use yii\base\Component;
use Craft;
use craft\web\Controller;
use craft\db\Query;
use craft\elements\Entry;
use craft\elements\Category;
use craft\services\Assets;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Interfaces\CacheInterface;
use BotMan\BotMan\Drivers\DriverManager;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Attachments\Location;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Conversations\Conversation;
use BotMan\Drivers\Facebook;
use BotMan\Drivers\Facebook\Extensions\ButtonTemplate;
use BotMan\Drivers\Facebook\Extensions\ElementButton;
use BotMan\Drivers\Facebook\Extensions\GenericTemplate;
use BotMan\Drivers\Facebook\Extensions\Element;
/**
* Default Controller
*
* Generally speaking, controllers are the middlemen between the front end of
* the CP/website and your plugin’s services. They contain action methods which
* handle individual tasks.
*
* A common pattern used throughout Craft involves a controller action gathering
* post data, saving it on a model, passing the model off to a service, and then
* responding to the request appropriately depending on the service method’s response.
*
* Action methods begin with the prefix “action”, followed by a description of what
* the method does (for example, actionSaveIngredient()).
*
* https://craftcms.com/docs/plugins/controllers
*
* @author eyekiller
* @package DeepRiverrock
* @since 1
*/
class ChatbotController extends Controller
{
// Protected Properties
// =========================================================================
/**
* @var bool|array Allows anonymous access to this controller's actions.
* The actions must be in 'kebab-case'
* @access protected
*/
protected $allowAnonymous = ['index'];
public $enableCsrfValidation = false;
// Public Methods
// =========================================================================
/**
* Handle a request going to our plugin's index action URL,
* e.g.: actions/deep-riverrock/default
*
* @return mixed
*/
public function actionIndex()
{
require '../vendor/autoload.php';
$config = [
'facebook' => [
'token' => 'xxxxx',
'verification' => 'xxxxx',
]
];
// Load the driver(s) you want to use
DriverManager::loadDriver(\BotMan\Drivers\Facebook\FacebookDriver::class);
$botman = BotManFactory::create($config, new Yii2Cache);
$botman->hears('GET_STARTED', function($bot) {
$bot->startConversation(new OnboardingConversation);
});
// Start listening
$botman->listen();
}
}
class Yii2Cache extends Component implements CacheInterface
{
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return $this->cache->exists($key);
}
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
file_put_contents('/Sites/cache.log', 'get(' . $key . ')', FILE_APPEND);
$value = $this->cache->get($key);
return ($value === false) ? $default : $value;
}
/**
* Retrieve an item from the cache and delete it.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function pull($key, $default = null)
{
file_put_contents('/Sites/cache.log', 'pull(' . $key . ')', FILE_APPEND);
$value = $this->get($key, $default);
$this->cache->delete($key);
return $value;
}
/**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param \DateTime|int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
file_put_contents('/Sites/cache.log', 'put(' . $key . ')', FILE_APPEND);
if ($minutes instanceof \Datetime) {
$seconds = $minutes->getTimestamp() - time();
} else {
$seconds = $minutes * 60;
}
$this->cache->set($key, $value, $seconds);
}
/**
* Gets the cache storage
*
* @return \yii\caching\CacheInterface
*/
public function getCache()
{
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
/** @var \yii\caching\CacheInterface $specificCache */
$specificCache = Yii::$app->get('botmanCache', false);
return $specificCache ?: Yii::$app->cache;
}
}
class OnboardingConversation extends Conversation
{
public function askLocation() {
$this->askForLocation('Please tell me your location.', function (Location $location) {
// $this->say('Received: '.print_r($location, true));
$this->say('Thanks! Here is a list of clubs near you. Select one to support.');
$this->askForClub();
}, null, [
'message' => [
'quick_replies' => json_encode([
[
'content_type' => 'location'
]
])
]
]);
}
public function run()
{
$this->askLocation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment