├── .env.dist ├── .github └── FUNDING.yml ├── .gitignore ├── README.md ├── app.json ├── bin ├── setup └── setup.php ├── composer.json ├── composer.lock └── webhook.php /.env.dist: -------------------------------------------------------------------------------- 1 | OPEN_WEATHER_MAP_API_KEY="" 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: mariojrrc 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QKK8XUG6WVCDU&source=url'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | .env 3 | .idea 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple PHP Webhook to respond Google Dialogflow requests. 2 | 3 | See complete post [here](https://medium.com/@mariojr.rcosta/como-construir-um-chatbot-do-whatsapp-usando-twilio-dialogflow-e-php-7a6eef4adcc2) 4 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "addons": [ 3 | 4 | ], 5 | "buildpacks": [ 6 | { 7 | "url": "heroku/php" 8 | } 9 | ], 10 | "env": { 11 | "OPEN_WEATHER_MAP_API_KEY": { 12 | "required": true 13 | } 14 | }, 15 | "formation": { 16 | "web": { 17 | "quantity": 1 18 | } 19 | }, 20 | "name": "dialogflow-php-webhook", 21 | "scripts": { 22 | }, 23 | "stack": "heroku-18" 24 | } 25 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | =5.3.0" 25 | }, 26 | "require-dev": { 27 | "phpunit/phpunit": "4.7.*" 28 | }, 29 | "type": "library", 30 | "extra": { 31 | "branch-alias": { 32 | "dev-master": "1.3-dev" 33 | } 34 | }, 35 | "autoload": { 36 | "psr-0": { 37 | "PhpOption\\": "src/" 38 | } 39 | }, 40 | "notification-url": "https://packagist.org/downloads/", 41 | "license": [ 42 | "Apache2" 43 | ], 44 | "authors": [ 45 | { 46 | "name": "Johannes M. Schmitt", 47 | "email": "schmittjoh@gmail.com" 48 | } 49 | ], 50 | "description": "Option Type for PHP", 51 | "keywords": [ 52 | "language", 53 | "option", 54 | "php", 55 | "type" 56 | ], 57 | "time": "2015-07-25T16:39:46+00:00" 58 | }, 59 | { 60 | "name": "symfony/polyfill-ctype", 61 | "version": "v1.10.0", 62 | "source": { 63 | "type": "git", 64 | "url": "https://github.com/symfony/polyfill-ctype.git", 65 | "reference": "e3d826245268269cd66f8326bd8bc066687b4a19" 66 | }, 67 | "dist": { 68 | "type": "zip", 69 | "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", 70 | "reference": "e3d826245268269cd66f8326bd8bc066687b4a19", 71 | "shasum": "" 72 | }, 73 | "require": { 74 | "php": ">=5.3.3" 75 | }, 76 | "suggest": { 77 | "ext-ctype": "For best performance" 78 | }, 79 | "type": "library", 80 | "extra": { 81 | "branch-alias": { 82 | "dev-master": "1.9-dev" 83 | } 84 | }, 85 | "autoload": { 86 | "psr-4": { 87 | "Symfony\\Polyfill\\Ctype\\": "" 88 | }, 89 | "files": [ 90 | "bootstrap.php" 91 | ] 92 | }, 93 | "notification-url": "https://packagist.org/downloads/", 94 | "license": [ 95 | "MIT" 96 | ], 97 | "authors": [ 98 | { 99 | "name": "Symfony Community", 100 | "homepage": "https://symfony.com/contributors" 101 | }, 102 | { 103 | "name": "Gert de Pagter", 104 | "email": "backendtea@gmail.com" 105 | } 106 | ], 107 | "description": "Symfony polyfill for ctype functions", 108 | "homepage": "https://symfony.com", 109 | "keywords": [ 110 | "compatibility", 111 | "ctype", 112 | "polyfill", 113 | "portable" 114 | ], 115 | "time": "2018-08-06T14:22:27+00:00" 116 | }, 117 | { 118 | "name": "vlucas/phpdotenv", 119 | "version": "v3.3.2", 120 | "source": { 121 | "type": "git", 122 | "url": "https://github.com/vlucas/phpdotenv.git", 123 | "reference": "1ee9369cfbf26cfcf1f2515d98f15fab54e9647a" 124 | }, 125 | "dist": { 126 | "type": "zip", 127 | "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/1ee9369cfbf26cfcf1f2515d98f15fab54e9647a", 128 | "reference": "1ee9369cfbf26cfcf1f2515d98f15fab54e9647a", 129 | "shasum": "" 130 | }, 131 | "require": { 132 | "php": "^5.4 || ^7.0", 133 | "phpoption/phpoption": "^1.5", 134 | "symfony/polyfill-ctype": "^1.9" 135 | }, 136 | "require-dev": { 137 | "phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0" 138 | }, 139 | "type": "library", 140 | "extra": { 141 | "branch-alias": { 142 | "dev-master": "3.3-dev" 143 | } 144 | }, 145 | "autoload": { 146 | "psr-4": { 147 | "Dotenv\\": "src/" 148 | } 149 | }, 150 | "notification-url": "https://packagist.org/downloads/", 151 | "license": [ 152 | "BSD-3-Clause" 153 | ], 154 | "authors": [ 155 | { 156 | "name": "Vance Lucas", 157 | "email": "vance@vancelucas.com", 158 | "homepage": "http://www.vancelucas.com" 159 | } 160 | ], 161 | "description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.", 162 | "keywords": [ 163 | "dotenv", 164 | "env", 165 | "environment" 166 | ], 167 | "time": "2019-01-30T10:43:17+00:00" 168 | } 169 | ], 170 | "packages-dev": [], 171 | "aliases": [], 172 | "minimum-stability": "stable", 173 | "stability-flags": [], 174 | "prefer-stable": false, 175 | "prefer-lowest": false, 176 | "platform": { 177 | "php": "^7.1", 178 | "ext-json": "*" 179 | }, 180 | "platform-dev": [] 181 | } 182 | -------------------------------------------------------------------------------- /webhook.php: -------------------------------------------------------------------------------- 1 | load(); 8 | 9 | /** 10 | * Envia uma requisição para a API do OpenWeatherMap e recupera 11 | * a informação do tempo para uma cidade 12 | * 13 | * @param string $city 14 | * @return string 15 | */ 16 | function getWeatherInformation(string $city): string 17 | { 18 | $apiKey = \getenv("OPEN_WEATHER_MAP_API_KEY"); 19 | $weatherUrl = "https://api.openweathermap.org/data/2.5/weather?q=$city&units=metric&appid=$apiKey&lang=pt"; 20 | $weather = \file_get_contents($weatherUrl); 21 | 22 | $weatherDetails = \json_decode($weather, true); 23 | 24 | $temperature = \round($weatherDetails["main"]["temp"] ?? 0, 1); 25 | $weatherDescription = $weatherDetails["weather"][0]["description"] ?? ''; 26 | 27 | return sendFulfillmentResponse($temperature, $weatherDescription); 28 | } 29 | 30 | /** 31 | * Envia a informação do tempo para o Dialogflow 32 | * 33 | * @param float $temperature 34 | * @param string $weatherDescription 35 | * 36 | * @return string 37 | */ 38 | function sendFulfillmentResponse(float $temperature, string $weatherDescription): string 39 | { 40 | $response = "Faz $temperature graus com $weatherDescription"; 41 | 42 | $fulfillment = [ 43 | "fulfillmentText" => $response 44 | ]; 45 | 46 | return \json_encode($fulfillment); 47 | } 48 | 49 | // listen to the POST request from Dialogflow 50 | $request = \file_get_contents("php://input"); 51 | $requestJson = \json_decode($request, true); 52 | 53 | $city = $requestJson['queryResult']['parameters']['geo-city'] ?? ''; 54 | 55 | if (isset($city) && !empty($city)) { 56 | echo getWeatherInformation($city); 57 | } 58 | --------------------------------------------------------------------------------