LoginSignup
2
0

More than 3 years have passed since last update.

Lambdaでお天気botを作ってます

Last updated at Posted at 2019-05-20

はじめに
お天気botと言っていますが、
悪天候でないと投稿されないよう条件にします。
【slackに投稿される条件】
・台風(最大風速)33m/s以上(気象庁より、強い台風と判別される)
・気温が前日比7度越え(最高気温と最低気温のどちらか)
・気圧が1004hPa以下(頭痛が発生する人もいる)
・最高気温と最低気温の差が10度以上

開発環境
・Lambda python3
・serverless framework
・npm

詳しい開発環境の設定方法は別の記事にします。

apiから天気情報を取得

無料で利用できるapiサービスを使用します。
OpenWeatherMap 
上記のサイトでアカウントを作成すると無料分のapiを取得できます。
デフォルトではjson形式で返ってきます。

今回は翌日の天気情報も欲しいので、5日間3時間毎の天気情報を取得します。

お天気情報の取得例


def open_weather_map():
    #open weather map のAPI
    api = "http://api.openweathermap.org/data/2.5/forecast?q=Tokyo,jp&&units=metric&APPID=XXXXXXXXXXXXXXXXXXXXXX"

    #APIからお天気データを取得
    response = requests.get(api)

    data = response.json()

    weatherInfo = json.dumps(data,indent=4)

    return weatherInfo

APPID=XXXXXXXXXXXXXXXXXXXXXX
自分のapiキーを入力して下さい。
サードパーティライブラリのrequestsを使用しています。
インストールしてからimportしておいて下さい。

これでapiから情報を取得できます。

天気を取得

お天気jsonから天気のみをパースします。

def weatherOnly(weather):
    dic = json.loads(weather)   
    description = dic['list'][10]['weather'][0]['description']
    return description

これで天気のみ取得できます。
ただ、天気は英語表記なので変換する必要があります。
apiに&lang=jaと付け加えておけば日本語で返ってきます。(descriptionのみ)
今回は絵文字で天気を表します。

def weatherOnly(weather):
    dic = json.loads(weather)   
    main = dic['list'][10]['weather'][0]['icon']
    emoji = weatherList(main)
    return emoji

def weatherList(content):
    List = {
        '01d':':sunny:',
        '02d':':barely_sunny:',
        '03d':':cloud:',
        '04d':':cloud:',
        '09d':':rain_cloud:',
        '10d':':sun_behind_rain_cloud:',
        '11d':':thunder_cloud_and_rain:',
        '13d':':snow_cloud:',
        '50d':':fog:',
        '01n':':sunny:',
        '02n':':barely_sunny:',
        '03n':':cloud:',
        '04n':':cloud:',
        '09n':':rain_cloud:',
        '10n':':sun_behind_rain_cloud:',
        '11n':':thunder_cloud_and_rain:',
        '13n':':snow_cloud:',
        '50n':':fog:'
    }
    data = List[content]
    return data

iconのidを利用して天気の絵文字に変換しました。
ちなみにiconのidには昼用と夜用があります。
数字のあとにdと付いているのが昼
数字のあとにnと付いているのが夜

これで天気は表示できました。

スクリーンショット 2018-11-12 15.38.11.png

台風(風速17m/s以上)の条件

強い台風の条件は風速が最大33k/mですので、
その条件付けをします。

def typhoon(content):

    dic = json.loads(content)
    #APIから風速を取得
    wind = dic['list'][10]['wind']['speed']

    #台風の条件
    if wind >= 33:
        #画面に表示するため整える
        wind = json.dumps(wind,indent=4)
        return wind
    else:
        return 0

風速33m/s以下であればslackに表示しないので0を返してOKです。

気温が前日に比べて7度越え

最低気温、最高気温のどちらも、あるいはどちらかが7度を超えていればslackに投稿します。

今日と明日の最高気温を比較

def maxTemp(content):

    dic = json.loads(content)

    today = 0
    tomorrow = 0

    #今日の最高気温
    for i in range(0,7):
        maxTempToday = dic['list'][i]['main']['temp_max']
        if maxTempToday > today:
            today = maxTempToday
    #翌日の最高気温
    for i in range(8,15):
        maxTempToday = dic['list'][i]['main']['temp_max']
        if maxTempToday > tomorrow:
            tomorrow = maxTempToday
    #最高気温前日比
    difference = today - tomorrow
    # 少数第3位を四捨五入する
    difference = round(difference,2)
    return(difference)

今日と明日の最低気温を比較


def minTemp(content):

    dic = json.loads(content)

    today = 100
    tomorrow = 100

    #今日の最低気温
    for i in range(0,7):
        minTemp = dic['list'][i]['main']['temp_min']
        if minTemp < today:
            today = minTemp
    #明日の最低気温
    for i in range(8,15):
        minTemp = dic['list'][i]['main']['temp_min']
        if minTemp < tomorrow:
            tomorrow = minTemp
    #最高気温前日比
    difference = today - tomorrow
    #少数第3位を四捨五入する
    difference = round(difference,2)
    return(difference)

これで最高気温、最低気温ともに差を取得できました。

今回はここまで。
続きは更新して行きます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0