Yioop_V9.5_Source_Code_Documentation

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
user_name

What is your name? your bot should respond with: Weatherbot (b) Request Expression: What is the weather in $location? Trigger State: 0 Remote Message: getWeather,$location Result State: 0 Response: $REMOTE_RESPONSE This second pattern will respond to queries of the form "What is the weather in $location?" For example, What is the weather in San Jose? It will then use the Bot Callback url to make a request of this script where the remote_message variable is set to getWeather,$location For example, in the case above, getWeather,San Jose The script below uses Yahoo's Api to get the weather for the request location and generate a response that it sends back to the chatbot in a variable $REMOTE_RESPONSE which it then uses in its response. This might look like: The weather is 55 and partly cloudy in San Jose. (c) Request Expression: Which is warmer $location1 or $location2? Trigger State: 0 Remote Message: getWarmer,$location1,$location2 Result State: 0 Response: $REMOTE_RESPONSE This pattern is similar to (b) except it demonstrate that you are allowed to have multiple variables in the Request Expression. This particular rules matches queries like: Which is warmer San Jose or Toronto? Sends a request to this script with a remote_message: getWarmer,San Jose,Toronto This script then generates a response that the bot receives as $REMOTE_RESPONSE and the bot echos it: San Jose at 56 is warmer than Toronto at 52 (8) Talk to your bot in yioop in this groups by logging in as a different user and posting a thread or commenting on a thread in a group you added the bot to with a message beginning with @user_name.

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
Return values
mixed

        

Search results