Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Getting Started
# A simple Chinese chatbot with weather request functionality using RASA framework

![](asset/xbot.jpg)
## Getting Started
Command line demo:
![](asset/example_1)

## Usage

1. Install require packages
```
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
poetry install
python -m pip install -U pip
pip3 install rasa-x --extra-index-url https://pypi.rasa.com/simple
```
Expand All @@ -22,13 +23,13 @@ refer to: [poetry documentation](https://python-poetry.org/docs/) and newest [of
Otherwise, your model will be saved at /models/default

```
rasa train --num-threads 4
rasa train --num-threads 4 # multi-threads for efficient computation
```

5. Run the raas action server:
5. Run the raas action server and interact with the bot on the command line interfact:

```
rasa run actions
rasa run actions & rasa shell
```


Expand Down
Binary file added actions/.DS_Store
Binary file not shown.
30 changes: 22 additions & 8 deletions actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from rasa_sdk.forms import FormAction

from actions import third_chat
from actions.third_weather import get_weather_by_day
# from actions.third_weather import get_weather_by_day
from actions.qweather import get_weather_by_day
from requests import (
ConnectionError,
HTTPError,
Expand Down Expand Up @@ -105,17 +106,30 @@ def get_text_weather_date(address, date_time, date_time_number):
except (ConnectionError, HTTPError, TooManyRedirects, Timeout) as e:
text_message = "{}".format(e)
else:
# text_message_tpl = """
# {} {} ({}) 的天气情况为:白天:{};夜晚:{};气温:{}-{} °C
# """
# text_message = text_message_tpl.format(
# result['location']['name'],
# date_time,
# result['result']['date'],
# result['result']['text_day'],
# result['result']['text_night'],
# result['result']["high"],
# result['result']["low"],
# )
text_message_tpl = """
{} {} ({}) 的天气情况为:白天:{};夜晚:{};气温:{}-{} °C
我掐指一算,{} {} ({}) 的天气情况为:白天:{};夜晚:{};气温:{}-{} °C;湿度:{}
"""
text_message = text_message_tpl.format(
result['location']['name'],
result['location'],
date_time,
result['result']['date'],
result['result']['text_day'],
result['result']['text_night'],
result['result']["high"],
result['result']["low"],
result['fxDate'],
result['textDay'],
result['textNight'],
result["tempMin"],
result["tempMax"],
result["humidity"]
)

return text_message
Expand Down
51 changes: 51 additions & 0 deletions actions/qweather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""
qweather.py
~~~~~~~~~
使用和风天气数据查询天气
"""

import requests
import json

KEY = '882e721338b4441dbf1b7e514029aaa8' # API key(私钥)
UID = "" # 用户ID, @todo: 当前并没有使用这个值,签名验证方式将使用到这个值

# LOCATION = 'beijing' # 所查询的位置,可以使用城市拼音、v3 ID、经纬度等
API_loc_id = 'https://geoapi.qweather.com/v2/city/lookup?' # 城市id查询
API = 'https://devapi.qweather.com/v7/weather/3d?' # API URL,可替换为其他 URL
# UNIT = 'c' # 单位
# LANGUAGE = 'zh-Hans' # 查询结果的返回语言

def fetch_location_id(location):
result = requests.get(API_loc_id, params={
'key': KEY,
'location': location,
})
return result.json()

def fetch_weather(location_id):
result = requests.get(API, params={
'key': KEY,
'location': location_id,
})
return result.json()

def get_weather_by_day(location, day=1):
location_id = fetch_location_id(location)["location"][0]["id"]
result = fetch_weather(location_id)
normal_result = {
"location": location,
"fxDate": result["daily"][day]["fxDate"],
"tempMax": result["daily"][day]["tempMax"],
"tempMin": result["daily"][day]["tempMin"],
"textDay": result["daily"][day]["textDay"],
"textNight": result["daily"][day]["textNight"],
"humidity": result["daily"][day]["humidity"]
}

return normal_result

if __name__ == "__main__":
print(get_weather_by_day('上海', 1))
print(json.dumps(get_weather_by_day('上海', 1), ensure_ascii=False))
Binary file added asset/.DS_Store
Binary file not shown.
Binary file added asset/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/example_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/.DS_Store
Binary file not shown.
Binary file added data/nlu/.DS_Store
Binary file not shown.
163 changes: 0 additions & 163 deletions data/nlu/number.yml

This file was deleted.

Binary file added data/story/.DS_Store
Binary file not shown.
Loading