bots: Move all bots and the bots API to separate package.
This commit is contained in:
parent
928d5ca16d
commit
879f44ab3a
130 changed files with 183 additions and 144 deletions
0
zulip_bots/zulip_bots/bots/weather/__init__.py
Normal file
0
zulip_bots/zulip_bots/bots/weather/__init__.py
Normal file
BIN
zulip_bots/zulip_bots/bots/weather/assets/screen1.png
Normal file
BIN
zulip_bots/zulip_bots/bots/weather/assets/screen1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 67 KiB |
BIN
zulip_bots/zulip_bots/bots/weather/assets/screen2.png
Normal file
BIN
zulip_bots/zulip_bots/bots/weather/assets/screen2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
16
zulip_bots/zulip_bots/bots/weather/readme.md
Normal file
16
zulip_bots/zulip_bots/bots/weather/readme.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# WeatherBot
|
||||
|
||||
* This is a bot that sends weather information to a selected stream on
|
||||
request.
|
||||
|
||||
* Weather information is brought to the website using an
|
||||
OpenWeatherMap API. The bot posts the weather information to the
|
||||
stream from which the user inputs the message. If the user inputs a
|
||||
city that does not exist, the bot displays a "Sorry, city not found"
|
||||
message.
|
||||
|
||||
* Before using this bot, you have to generate an OpenWeatherMap API
|
||||
key and replace the dummy value in weather.conf.
|
||||
|
||||

|
||||

|
2
zulip_bots/zulip_bots/bots/weather/weather.conf
Normal file
2
zulip_bots/zulip_bots/bots/weather/weather.conf
Normal file
|
@ -0,0 +1,2 @@
|
|||
[weather-config]
|
||||
key=XXXXXXXX
|
58
zulip_bots/zulip_bots/bots/weather/weather.py
Normal file
58
zulip_bots/zulip_bots/bots/weather/weather.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
# See readme.md for instructions on running this code.
|
||||
from __future__ import print_function
|
||||
import requests
|
||||
import json
|
||||
|
||||
class WeatherHandler(object):
|
||||
def initialize(self, bot_handler):
|
||||
self.api_key = bot_handler.get_config_info('weather', 'weather-config')['key']
|
||||
self.response_pattern = 'Weather in {}, {}:\n{} F / {} C\n{}'
|
||||
|
||||
def usage(self):
|
||||
return '''
|
||||
This plugin will give info about weather in a specified city
|
||||
'''
|
||||
|
||||
def handle_message(self, message, bot_handler, state_handler):
|
||||
help_content = '''
|
||||
This bot returns weather info for specified city.
|
||||
You specify city in the following format:
|
||||
city, state/country
|
||||
state and country parameter is optional(useful when there are many cities with the same name)
|
||||
For example:
|
||||
@**Weather Bot** Portland
|
||||
@**Weather Bot** Portland, Me
|
||||
'''.strip()
|
||||
|
||||
if (message['content'] == 'help') or (message['content'] == ''):
|
||||
response = help_content
|
||||
else:
|
||||
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + message['content'] + '&APPID='
|
||||
r = requests.get(url + self.api_key)
|
||||
if "city not found" in r.text:
|
||||
response = "Sorry, city not found"
|
||||
else:
|
||||
response = format_response(r.text, message['content'], self.response_pattern)
|
||||
|
||||
bot_handler.send_reply(message, response)
|
||||
|
||||
|
||||
def format_response(text, city, response_pattern):
|
||||
j = json.loads(text)
|
||||
city = j['name']
|
||||
country = j['sys']['country']
|
||||
fahrenheit = to_fahrenheit(j['main']['temp'])
|
||||
celsius = to_celsius(j['main']['temp'])
|
||||
description = j['weather'][0]['description'].title()
|
||||
|
||||
return response_pattern.format(city, country, fahrenheit, celsius, description)
|
||||
|
||||
|
||||
def to_celsius(temp_kelvin):
|
||||
return int(temp_kelvin) - 273.15
|
||||
|
||||
|
||||
def to_fahrenheit(temp_kelvin):
|
||||
return int(temp_kelvin) * 9 / 5 - 459.67
|
||||
|
||||
handler_class = WeatherHandler
|
Loading…
Add table
Add a link
Reference in a new issue