WeatherBot
in package
This class demonstrates a simple Weather Chat Bot using the Yioop ChatBot APIs for Yioop Discussion Groups.
To use this bot: (0) Move this file to some folder of a web server you have access to. Denote by some_url the url of this folder/WeatherBot.php If you point your browser at this url you should see a message that begins with: There was a configuration issue with your query. (1) Get an api key from openweathermap.org and fill it in below. (2) Create a new Yioop User. (3) Under Manage Accounts, click on the gear symbol next to Account Details (4) Check the Bot User check box, click save. (5) Two form variables should appear: Bot Unique Token and Bot Callback URL. Fill in a value for Bot Unique Token that matches the value set for ACCESS_TOKEN in the code within the WeatherBot class. Fill in some_url (as defined in step (1)) for the value of Bot Callback URL (6) Add the the user you created in Yioop to the group that you would like the bot to service. Let the name of this user be user_name. (7) When logged into the user_name account you should now see a Bot Story activity in the activity side bar. Click on Bot Story. This activity lets you add patterns that tell your chat bot how to react when it sees various expressions. To demo your bot add the following three patterns: (a) Request Expression: What is your name? Trigger State: 0 Remote Message: Result State: 0 Response: Weatherbot This first pattern just says if someone does a query of the form:
Tags
Table of Contents
- ACCESS_TOKEN = "1234"
- Token given when setting up the bot in Yioop for callback requests This bots checks that a request from a Yioop Instance sends a timestamp as well as the hash of this timestamp with the bot_token and post data and that these match the expected values
- API_KEY = ""
- API KEY of site that this bot gets weather information from
- TIME_WINDOW = 60
- Number of seconds that the passed timestamp can differ from the current time on the WeatherBot machine.
- UNITS = "imperial"
- metric, default, or imperial units for temperature
- WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather"
- Url of site that this bot gets weather information from
- checkBotToken() : mixed
- This method is used to check a request that it comes from a site that knows the bot_token in use by this WeatherBot.
- getRawWeatherData() : object
- Get json data about a location from weather service
- getWarmer() : string
- Return which location is warmer, the one stored in $args[0] or the one stored in $args[1]
- getWeather() : string
- Get weather information about a location
- processRequest() : mixed
- This is the method called to get the WeatherBot to handle an incoming HTTP request, and echo a weather related message
Constants
ACCESS_TOKEN
Token given when setting up the bot in Yioop for callback requests This bots checks that a request from a Yioop Instance sends a timestamp as well as the hash of this timestamp with the bot_token and post data and that these match the expected values
public
mixed
ACCESS_TOKEN
= "1234"
API_KEY
API KEY of site that this bot gets weather information from
public
mixed
API_KEY
= ""
TIME_WINDOW
Number of seconds that the passed timestamp can differ from the current time on the WeatherBot machine.
public
mixed
TIME_WINDOW
= 60
UNITS
metric, default, or imperial units for temperature
public
mixed
UNITS
= "imperial"
WEATHER_URL
Url of site that this bot gets weather information from
public
mixed
WEATHER_URL
= "https://api.openweathermap.org/data/2.5/weather"
Methods
checkBotToken()
This method is used to check a request that it comes from a site that knows the bot_token in use by this WeatherBot.
public
checkBotToken() : mixed
Return values
mixed —getRawWeatherData()
Get json data about a location from weather service
public
getRawWeatherData(array<string|int, mixed> $location) : object
Parameters
- $location : array<string|int, mixed>
-
to get weather update for
Return values
object —weather information
getWarmer()
Return which location is warmer, the one stored in $args[0] or the one stored in $args[1]
public
getWarmer(array<string|int, mixed> $args) : string
Parameters
- $args : array<string|int, mixed>
-
the value of $args[0] should have the location to get weather update for
Return values
string —weather information
getWeather()
Get weather information about a location
public
getWeather(array<string|int, mixed> $args) : string
Parameters
- $args : array<string|int, mixed>
-
the value of $args[0] should have the location to get weather update for
Return values
string —weather information
processRequest()
This is the method called to get the WeatherBot to handle an incoming HTTP request, and echo a weather related message
public
processRequest() : mixed