-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import getWeather from './weather'; | ||
|
||
export const plugins = [getWeather]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import runner from './runner'; | ||
|
||
const schema = { | ||
description: '获取当前天气情况', | ||
name: 'fetchWeather', | ||
parameters: { | ||
properties: { | ||
city: { | ||
description: '城市名称', | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['city'], | ||
type: 'object', | ||
}, | ||
}; | ||
|
||
const getWeather = { name: 'fetchWeather', runner, schema }; | ||
|
||
export default getWeather; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const weatherBaseURL = 'https://restapi.amap.com/v3/weather/weatherInfo'; | ||
|
||
const citySearchURL = 'https://restapi.amap.com/v3/config/district'; | ||
|
||
const KEY = process.env.GAODE_WEATHER_KEY; | ||
|
||
interface WeatherParams { | ||
city: string; | ||
extensions?: 'base' | 'all'; | ||
} | ||
|
||
const fetchCityCode = async (keywords: string): Promise<string> => { | ||
const URL = `${citySearchURL}?keywords=${keywords}&subdistrict=0&extensions=base&key=${KEY}`; | ||
const res = await fetch(URL); | ||
|
||
const data = await res.json(); | ||
|
||
return data.districts[0].adcode; | ||
}; | ||
|
||
const fetchWeather = async ({ city, extensions = 'all' }: WeatherParams) => { | ||
const cityCode = await fetchCityCode(city); | ||
|
||
const URL = `${weatherBaseURL}?city=${cityCode}&extensions=${extensions}&key=${KEY}`; | ||
const res = await fetch(URL); | ||
|
||
return await res.json(); | ||
}; | ||
|
||
export default fetchWeather; |