前記事:「位置情報を送ると天気予報を教えてくれるLINE botを作成してみました」では、142件もの地域コードをソース上で条件分岐させていたのですが、
HerokuのHeroku Postrageを使用することで、地域コードマスタをDB管理することが出来ましたので、私の方で実施した内容をご紹介したいと思います。
BOTの名前は、weとweatherとtogetherをもじって、"we-together"です。
こちらのBOTのQRコードです。
※前記事にも記載していますが、このBOTはLINEの位置情報のテキストを条件に
天気情報API連携で天気予報の情報を取得しているので、位置情報によっては条件に合致せずに表示されないことがあります。
環境
- Windows 10
- Node.js 12.16.1
- Heroku Postgresql 12.2-2
事前設定
「Node.jsアプリケーションをHerokuにデプロイ」を参考にHerokuの設定をしています。
「HerokuのCLIダウンロードからデプロイまで」を参考にHeroku CLIの設定しています。
「Herokuにおける無料のDBを利用する方法」を参考にHeroku DBの設定しています。
開発
今回も前記事同様、LINEの位置情報のアドレス(address)を用いていきます。
以下、参考までに東京ディズニーランドのLINE位置情報です。
東京ディズニーランドのLINE位置情報
type: 'message',
replyToken: 'cc93a3636cf54f218a924f63ab308d83',
source: { userId: 'hogehoge', type: 'user' },
timestamp: 1585812598044,
mode: 'active',
message: {
type: 'location',
id: '11711207573900',
title: '東京ディズニーリゾート',
address: '舞浜1-1 浦安市, 千葉県 279-8511 日本',
latitude: 35.630668,
longitude: 139.882873
latitude: 35.630668,
longitude: 139.882873
address: '舞浜1-1 浦安市, 千葉県 279-8511 日本'
ロジックとしては、まず、位置情報のアドレスに"千葉"という文言が1件でもあれば、
"120010"(city_id)を取得します。
if(address.indexOf("千葉") > -1){
city_id = '120010'; // 千葉
取得したcity_idを用いて
const res = await axios.get('http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city_id);
http://weather.livedoor.com/forecast/webservice/json/v1?city=120010
という形にして、
そこから必要な天気情報を取得しています。
LINE BOT(we-together)の動き
但し、前回の記事では地域コードマスタをDB管理せず、それでも全国に対応できるように、以下の通り142件を条件分岐を作っていました。
DB管理する前のソース(抜粋)
console.log("address : " + address);
// 無茶苦茶、強引な検索
if(address.indexOf("稚内") > -1){city_id = '011000'; // 稚内
} else if(address.indexOf("旭川") > -1){city_id = '012010'; // 旭川
} else if(address.indexOf("留萌") > -1){city_id = '012020'; // 留萌
} else if(address.indexOf("網走") > -1){city_id = '013010'; // 網走
} else if(address.indexOf("北見") > -1){city_id = '013020'; // 北見
} else if(address.indexOf("紋別") > -1){city_id = '013030'; // 紋別
} else if(address.indexOf("根室") > -1){city_id = '014010'; // 根室
} else if(address.indexOf("釧路") > -1){city_id = '014020'; // 釧路
} else if(address.indexOf("帯広") > -1){city_id = '014030'; // 帯広
} else if(address.indexOf("室蘭") > -1){city_id = '015010'; // 室蘭
} else if(address.indexOf("浦河") > -1){city_id = '015020'; // 浦河
} else if(address.indexOf("札幌") > -1){city_id = '016010'; // 札幌
} else if(address.indexOf("岩見沢") > -1){city_id = '016020'; // 岩見沢
} else if(address.indexOf("倶知安") > -1){city_id = '016030'; // 倶知安
} else if(address.indexOf("函館") > -1){city_id = '017010'; // 函館
} else if(address.indexOf("江差") > -1){city_id = '017020'; // 江差
} else if(address.indexOf("青森") > -1){city_id = '020010'; // 青森
} else if(address.indexOf("むつ") > -1){city_id = '020020'; // むつ
} else if(address.indexOf("八戸") > -1){city_id = '020030'; // 八戸
} else if(address.indexOf("盛岡") > -1){city_id = '030010'; // 盛岡
} else if(address.indexOf("宮古") > -1){city_id = '030020'; // 宮古
} else if(address.indexOf("大船渡") > -1){city_id = '030030'; // 大船渡
} else if(address.indexOf("仙台") > -1){city_id = '040010'; // 仙台
} else if(address.indexOf("白石") > -1){city_id = '040020'; // 白石
} else if(address.indexOf("秋田") > -1){city_id = '050010'; // 秋田
} else if(address.indexOf("横手") > -1){city_id = '050020'; // 横手
} else if(address.indexOf("山形") > -1){city_id = '060010'; // 山形
} else if(address.indexOf("米沢") > -1){city_id = '060020'; // 米沢
} else if(address.indexOf("酒田") > -1){city_id = '060030'; // 酒田
} else if(address.indexOf("新庄") > -1){city_id = '060040'; // 新庄
} else if(address.indexOf("福島") > -1){city_id = '070010'; // 福島
} else if(address.indexOf("小名浜") > -1){city_id = '070020'; // 小名浜
} else if(address.indexOf("若松") > -1){city_id = '070030'; // 若松
} else if(address.indexOf("水戸") > -1){city_id = '080010'; // 水戸
} else if(address.indexOf("土浦") > -1){city_id = '080020'; // 土浦
} else if(address.indexOf("宇都宮") > -1){city_id = '090010'; // 宇都宮
} else if(address.indexOf("大田原") > -1){city_id = '090020'; // 大田原
} else if(address.indexOf("前橋") > -1){city_id = '100010'; // 前橋
} else if(address.indexOf("みなかみ") > -1){city_id = '100020'; // みなかみ
} else if(address.indexOf("さいたま") > -1){city_id = '110010'; // さいたま
} else if(address.indexOf("熊谷") > -1){city_id = '110020'; // 熊谷
} else if(address.indexOf("秩父") > -1){city_id = '110030'; // 秩父
} else if(address.indexOf("千葉") > -1){city_id = '120010'; // 千葉
} else if(address.indexOf("銚子") > -1){city_id = '120020'; // 銚子
} else if(address.indexOf("館山") > -1){city_id = '120030'; // 館山
} else if(address.indexOf("東京") > -1){city_id = '130010'; // 東京
} else if(address.indexOf("大島") > -1){city_id = '130020'; // 大島
} else if(address.indexOf("八丈島") > -1){city_id = '130030'; // 八丈島
} else if(address.indexOf("父島") > -1){city_id = '130040'; // 父島
} else if(address.indexOf("横浜") > -1){city_id = '140010'; // 横浜
} else if(address.indexOf("小田原") > -1){city_id = '140020'; // 小田原
} else if(address.indexOf("新潟") > -1){city_id = '150010'; // 新潟
} else if(address.indexOf("長岡") > -1){city_id = '150020'; // 長岡
} else if(address.indexOf("高田") > -1){city_id = '150030'; // 高田
} else if(address.indexOf("相川") > -1){city_id = '150040'; // 相川
} else if(address.indexOf("富山") > -1){city_id = '160010'; // 富山
} else if(address.indexOf("伏木") > -1){city_id = '160020'; // 伏木
} else if(address.indexOf("金沢") > -1){city_id = '170010'; // 金沢
} else if(address.indexOf("輪島") > -1){city_id = '170020'; // 輪島
} else if(address.indexOf("福井") > -1){city_id = '180010'; // 福井
} else if(address.indexOf("敦賀") > -1){city_id = '180020'; // 敦賀
} else if(address.indexOf("甲府") > -1){city_id = '190010'; // 甲府
} else if(address.indexOf("河口湖") > -1){city_id = '190020'; // 河口湖
} else if(address.indexOf("長野") > -1){city_id = '200010'; // 長野
} else if(address.indexOf("松本") > -1){city_id = '200020'; // 松本
} else if(address.indexOf("飯田") > -1){city_id = '200030'; // 飯田
} else if(address.indexOf("岐阜") > -1){city_id = '210010'; // 岐阜
} else if(address.indexOf("高山") > -1){city_id = '210020'; // 高山
} else if(address.indexOf("静岡") > -1){city_id = '220010'; // 静岡
} else if(address.indexOf("網代") > -1){city_id = '220020'; // 網代
} else if(address.indexOf("三島") > -1){city_id = '220030'; // 三島
} else if(address.indexOf("浜松") > -1){city_id = '220040'; // 浜松
} else if(address.indexOf("名古屋") > -1){city_id = '230010'; // 名古屋
} else if(address.indexOf("豊橋") > -1){city_id = '230020'; // 豊橋
} else if(address.indexOf("津") > -1){city_id = '240010'; // 津
} else if(address.indexOf("尾鷲") > -1){city_id = '240020'; // 尾鷲
} else if(address.indexOf("大津") > -1){city_id = '250010'; // 大津
} else if(address.indexOf("彦根") > -1){city_id = '250020'; // 彦根
} else if(address.indexOf("京都") > -1){city_id = '260010'; // 京都
} else if(address.indexOf("舞鶴") > -1){city_id = '260020'; // 舞鶴
} else if(address.indexOf("大阪") > -1){city_id = '270000'; // 大阪
} else if(address.indexOf("神戸") > -1){city_id = '280010'; // 神戸
} else if(address.indexOf("豊岡") > -1){city_id = '280020'; // 豊岡
} else if(address.indexOf("奈良") > -1){city_id = '290010'; // 奈良
} else if(address.indexOf("風屋") > -1){city_id = '290020'; // 風屋
} else if(address.indexOf("和歌山") > -1){city_id = '300010'; // 和歌山
} else if(address.indexOf("潮岬") > -1){city_id = '300020'; // 潮岬
} else if(address.indexOf("鳥取") > -1){city_id = '310010'; // 鳥取
} else if(address.indexOf("米子") > -1){city_id = '310020'; // 米子
} else if(address.indexOf("松江") > -1){city_id = '320010'; // 松江
} else if(address.indexOf("浜田") > -1){city_id = '320020'; // 浜田
} else if(address.indexOf("西郷") > -1){city_id = '320030'; // 西郷
} else if(address.indexOf("岡山") > -1){city_id = '330010'; // 岡山
} else if(address.indexOf("津山") > -1){city_id = '330020'; // 津山
} else if(address.indexOf("広島") > -1){city_id = '340010'; // 広島
} else if(address.indexOf("庄原") > -1){city_id = '340020'; // 庄原
} else if(address.indexOf("下関") > -1){city_id = '350010'; // 下関
} else if(address.indexOf("山口") > -1){city_id = '350020'; // 山口
} else if(address.indexOf("柳井") > -1){city_id = '350030'; // 柳井
} else if(address.indexOf("萩") > -1){city_id = '350040'; // 萩
} else if(address.indexOf("徳島") > -1){city_id = '360010'; // 徳島
} else if(address.indexOf("日和佐") > -1){city_id = '360020'; // 日和佐
} else if(address.indexOf("高松") > -1){city_id = '370000'; // 高松
} else if(address.indexOf("松山") > -1){city_id = '380010'; // 松山
} else if(address.indexOf("新居浜") > -1){city_id = '380020'; // 新居浜
} else if(address.indexOf("宇和島") > -1){city_id = '380030'; // 宇和島
} else if(address.indexOf("高知") > -1){city_id = '390010'; // 高知
} else if(address.indexOf("室戸岬") > -1){city_id = '390020'; // 室戸岬
} else if(address.indexOf("清水") > -1){city_id = '390030'; // 清水
} else if(address.indexOf("福岡") > -1){city_id = '400010'; // 福岡
} else if(address.indexOf("八幡") > -1){city_id = '400020'; // 八幡
} else if(address.indexOf("飯塚") > -1){city_id = '400030'; // 飯塚
} else if(address.indexOf("久留米") > -1){city_id = '400040'; // 久留米
} else if(address.indexOf("佐賀") > -1){city_id = '410010'; // 佐賀
} else if(address.indexOf("伊万里") > -1){city_id = '410020'; // 伊万里
} else if(address.indexOf("長崎") > -1){city_id = '420010'; // 長崎
} else if(address.indexOf("佐世保") > -1){city_id = '420020'; // 佐世保
} else if(address.indexOf("厳原") > -1){city_id = '420030'; // 厳原
} else if(address.indexOf("福江") > -1){city_id = '420040'; // 福江
} else if(address.indexOf("熊本") > -1){city_id = '430010'; // 熊本
} else if(address.indexOf("阿蘇乙姫") > -1){city_id = '430020'; // 阿蘇乙姫
} else if(address.indexOf("牛深") > -1){city_id = '430030'; // 牛深
} else if(address.indexOf("人吉") > -1){city_id = '430040'; // 人吉
} else if(address.indexOf("大分") > -1){city_id = '440010'; // 大分
} else if(address.indexOf("中津") > -1){city_id = '440020'; // 中津
} else if(address.indexOf("日田") > -1){city_id = '440030'; // 日田
} else if(address.indexOf("佐伯") > -1){city_id = '440040'; // 佐伯
} else if(address.indexOf("宮崎") > -1){city_id = '450010'; // 宮崎
} else if(address.indexOf("延岡") > -1){city_id = '450020'; // 延岡
} else if(address.indexOf("都城") > -1){city_id = '450030'; // 都城
} else if(address.indexOf("高千穂") > -1){city_id = '450040'; // 高千穂
} else if(address.indexOf("鹿児島") > -1){city_id = '460010'; // 鹿児島
} else if(address.indexOf("鹿屋") > -1){city_id = '460020'; // 鹿屋
} else if(address.indexOf("種子島") > -1){city_id = '460030'; // 種子島
} else if(address.indexOf("名瀬") > -1){city_id = '460040'; // 名瀬
} else if(address.indexOf("那覇") > -1){city_id = '471010'; // 那覇
} else if(address.indexOf("名護") > -1){city_id = '471020'; // 名護
} else if(address.indexOf("久米島") > -1){city_id = '471030'; // 久米島
} else if(address.indexOf("南大東") > -1){city_id = '472000'; // 南大東
} else if(address.indexOf("宮古島") > -1){city_id = '473000'; // 宮古島
} else if(address.indexOf("石垣島") > -1){city_id = '474010'; // 石垣島
} else if(address.indexOf("与那国島") > -1){city_id = '474020'; // 与那国島
}
const res = await axios.get('http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city_id);
const item = res.data;
長いですね。
今回、これをHerokuのDBを利用して管理したいと思います。
まず、以下を実行して、Herokuのpostgresに接続します。
heroku pg:psql -a [アプリ名]
ログ上に、Connecting to...と表示されることを確認します。
次に以下を実行してテーブルを作成(create)します。
今回作成したいテーブル(area_tbl)は、
AREA_CODE | AREA_NAME |
---|---|
011000 | 稚内 |
012010 | 旭川 |
012020 | 留萌 |
013010 | 網走 |
013020 | 北見 |
013030 | 紋別 |
014010 | 根室 |
014020 | 釧路 |
014030 | 帯広 |
015010 | 室蘭 |
015020 | 浦河 |
016010 | 札幌 |
016020 | 岩見沢 |
016030 | 倶知安 |
017010 | 函館 |
017020 | 江差 |
020010 | 青森 |
020020 | むつ |
020030 | 八戸 |
030010 | 盛岡 |
030020 | 宮古 |
030030 | 大船渡 |
040010 | 仙台 |
040020 | 白石 |
050010 | 秋田 |
050020 | 横手 |
060010 | 山形 |
060020 | 米沢 |
060030 | 酒田 |
060040 | 新庄 |
070010 | 福島 |
070020 | 小名浜 |
070030 | 若松 |
080010 | 水戸 |
080020 | 土浦 |
090010 | 宇都宮 |
090020 | 大田原 |
100010 | 前橋 |
100020 | みなかみ |
110010 | さいたま |
110020 | 熊谷 |
110030 | 秩父 |
120010 | 千葉 |
120020 | 銚子 |
120030 | 館山 |
130010 | 東京 |
130020 | 大島 |
130030 | 八丈島 |
130040 | 父島 |
140010 | 横浜 |
140020 | 小田原 |
150010 | 新潟 |
150020 | 長岡 |
150030 | 高田 |
150040 | 相川 |
160010 | 富山 |
160020 | 伏木 |
170010 | 金沢 |
170020 | 輪島 |
180010 | 福井 |
180020 | 敦賀 |
190010 | 甲府 |
190020 | 河口湖 |
200010 | 長野 |
200020 | 松本 |
200030 | 飯田 |
210010 | 岐阜 |
210020 | 高山 |
220010 | 静岡 |
220020 | 網代 |
220030 | 三島 |
220040 | 浜松 |
230010 | 名古屋 |
230020 | 豊橋 |
240010 | 津 |
240020 | 尾鷲 |
250010 | 大津 |
250020 | 彦根 |
260010 | 京都 |
260020 | 舞鶴 |
270000 | 大阪 |
280010 | 神戸 |
280020 | 豊岡 |
290010 | 奈良 |
290020 | 風屋 |
300010 | 和歌山 |
300020 | 潮岬 |
310010 | 鳥取 |
310020 | 米子 |
320010 | 松江 |
320020 | 浜田 |
320030 | 西郷 |
330010 | 岡山 |
330020 | 津山 |
340010 | 広島 |
340020 | 庄原 |
350010 | 下関 |
350020 | 山口 |
350030 | 柳井 |
350040 | 萩 |
360010 | 徳島 |
360020 | 日和佐 |
370000 | 高松 |
380010 | 松山 |
380020 | 新居浜 |
380030 | 宇和島 |
390010 | 高知 |
390020 | 室戸岬 |
390030 | 清水 |
400010 | 福岡 |
400020 | 八幡 |
400030 | 飯塚 |
400040 | 久留米 |
410010 | 佐賀 |
410020 | 伊万里 |
420010 | 長崎 |
420020 | 佐世保 |
420030 | 厳原 |
420040 | 福江 |
430010 | 熊本 |
430020 | 阿蘇乙姫 |
430030 | 牛深 |
430040 | 人吉 |
440010 | 大分 |
440020 | 中津 |
440030 | 日田 |
440040 | 佐伯 |
450010 | 宮崎 |
450020 | 延岡 |
450030 | 都城 |
450040 | 高千穂 |
460010 | 鹿児島 |
460020 | 鹿屋 |
460030 | 種子島 |
460040 | 名瀬 |
471010 | 那覇 |
471020 | 名護 |
471030 | 久米島 |
472000 | 南大東 |
473000 | 宮古島 |
474010 | 石垣島 |
474020 | 与那国島 |
計142件
のようなデータです。
以下を実行します。
テーブル作成
CREATE TABLE area_tbl(
area_id INTEGER NOT NULL,
area_name VARCHAR(6) NOT NULL,
PRIMARY KEY(AREA_ID)
);
ログ上に、"CREATE TABLE"と表示されることを確認します。
(/dでも確認が出来ます)
テーブルが作成できたことを確認出来たら、レコード挿入(INSERT)を行います。
レコード挿入
INSERT INTO area_tbl(area_id, area_name) VALUES (011000,'稚内');
ログ上に、"INSERT 0 1"と表示されることを確認します。
問題なければ、
同様に以下残りのレコード挿入も行います。
INSERT INTO area_tbl(area_id, area_name) VALUES (012010,'旭川');
INSERT INTO area_tbl(area_id, area_name) VALUES (012020,'留萌');
INSERT INTO area_tbl(area_id, area_name) VALUES (013010,'網走');
INSERT INTO area_tbl(area_id, area_name) VALUES (013020,'北見');
INSERT INTO area_tbl(area_id, area_name) VALUES (013030,'紋別');
INSERT INTO area_tbl(area_id, area_name) VALUES (014010,'根室');
INSERT INTO area_tbl(area_id, area_name) VALUES (014020,'釧路');
INSERT INTO area_tbl(area_id, area_name) VALUES (014030,'帯広');
INSERT INTO area_tbl(area_id, area_name) VALUES (015010,'室蘭');
INSERT INTO area_tbl(area_id, area_name) VALUES (015020,'浦河');
INSERT INTO area_tbl(area_id, area_name) VALUES (016010,'札幌');
INSERT INTO area_tbl(area_id, area_name) VALUES (016020,'岩見沢');
INSERT INTO area_tbl(area_id, area_name) VALUES (016030,'倶知安');
INSERT INTO area_tbl(area_id, area_name) VALUES (017010,'函館');
INSERT INTO area_tbl(area_id, area_name) VALUES (017020,'江差');
INSERT INTO area_tbl(area_id, area_name) VALUES (020010,'青森');
INSERT INTO area_tbl(area_id, area_name) VALUES (020020,'むつ');
INSERT INTO area_tbl(area_id, area_name) VALUES (020030,'八戸');
INSERT INTO area_tbl(area_id, area_name) VALUES (030010,'盛岡');
INSERT INTO area_tbl(area_id, area_name) VALUES (030020,'宮古');
INSERT INTO area_tbl(area_id, area_name) VALUES (030030,'大船渡');
INSERT INTO area_tbl(area_id, area_name) VALUES (040010,'仙台');
INSERT INTO area_tbl(area_id, area_name) VALUES (040020,'白石');
INSERT INTO area_tbl(area_id, area_name) VALUES (050010,'秋田');
INSERT INTO area_tbl(area_id, area_name) VALUES (050020,'横手');
INSERT INTO area_tbl(area_id, area_name) VALUES (060010,'山形');
INSERT INTO area_tbl(area_id, area_name) VALUES (060020,'米沢');
INSERT INTO area_tbl(area_id, area_name) VALUES (060030,'酒田');
INSERT INTO area_tbl(area_id, area_name) VALUES (060040,'新庄');
INSERT INTO area_tbl(area_id, area_name) VALUES (070010,'福島');
INSERT INTO area_tbl(area_id, area_name) VALUES (070020,'小名浜');
INSERT INTO area_tbl(area_id, area_name) VALUES (070030,'若松');
INSERT INTO area_tbl(area_id, area_name) VALUES (080010,'水戸');
INSERT INTO area_tbl(area_id, area_name) VALUES (080020,'土浦');
INSERT INTO area_tbl(area_id, area_name) VALUES (090010,'宇都宮');
INSERT INTO area_tbl(area_id, area_name) VALUES (090020,'大田原');
INSERT INTO area_tbl(area_id, area_name) VALUES (100010,'前橋');
INSERT INTO area_tbl(area_id, area_name) VALUES (100020,'みなかみ');
INSERT INTO area_tbl(area_id, area_name) VALUES (110010,'さいたま');
INSERT INTO area_tbl(area_id, area_name) VALUES (110020,'熊谷');
INSERT INTO area_tbl(area_id, area_name) VALUES (110030,'秩父');
INSERT INTO area_tbl(area_id, area_name) VALUES (120010,'千葉');
INSERT INTO area_tbl(area_id, area_name) VALUES (120020,'銚子');
INSERT INTO area_tbl(area_id, area_name) VALUES (120030,'館山');
INSERT INTO area_tbl(area_id, area_name) VALUES (130010,'東京');
INSERT INTO area_tbl(area_id, area_name) VALUES (130020,'大島');
INSERT INTO area_tbl(area_id, area_name) VALUES (130030,'八丈島');
INSERT INTO area_tbl(area_id, area_name) VALUES (130040,'父島');
INSERT INTO area_tbl(area_id, area_name) VALUES (140010,'横浜');
INSERT INTO area_tbl(area_id, area_name) VALUES (140020,'小田原');
INSERT INTO area_tbl(area_id, area_name) VALUES (150010,'新潟');
INSERT INTO area_tbl(area_id, area_name) VALUES (150020,'長岡');
INSERT INTO area_tbl(area_id, area_name) VALUES (150030,'高田');
INSERT INTO area_tbl(area_id, area_name) VALUES (150040,'相川');
INSERT INTO area_tbl(area_id, area_name) VALUES (160010,'富山');
INSERT INTO area_tbl(area_id, area_name) VALUES (160020,'伏木');
INSERT INTO area_tbl(area_id, area_name) VALUES (170010,'金沢');
INSERT INTO area_tbl(area_id, area_name) VALUES (170020,'輪島');
INSERT INTO area_tbl(area_id, area_name) VALUES (180010,'福井');
INSERT INTO area_tbl(area_id, area_name) VALUES (180020,'敦賀');
INSERT INTO area_tbl(area_id, area_name) VALUES (190010,'甲府');
INSERT INTO area_tbl(area_id, area_name) VALUES (190020,'河口湖');
INSERT INTO area_tbl(area_id, area_name) VALUES (200010,'長野');
INSERT INTO area_tbl(area_id, area_name) VALUES (200020,'松本');
INSERT INTO area_tbl(area_id, area_name) VALUES (200030,'飯田');
INSERT INTO area_tbl(area_id, area_name) VALUES (210010,'岐阜');
INSERT INTO area_tbl(area_id, area_name) VALUES (210020,'高山');
INSERT INTO area_tbl(area_id, area_name) VALUES (220010,'静岡');
INSERT INTO area_tbl(area_id, area_name) VALUES (220020,'網代');
INSERT INTO area_tbl(area_id, area_name) VALUES (220030,'三島');
INSERT INTO area_tbl(area_id, area_name) VALUES (220040,'浜松');
INSERT INTO area_tbl(area_id, area_name) VALUES (230010,'名古屋');
INSERT INTO area_tbl(area_id, area_name) VALUES (230020,'豊橋');
INSERT INTO area_tbl(area_id, area_name) VALUES (240010,'津');
INSERT INTO area_tbl(area_id, area_name) VALUES (240020,'尾鷲');
INSERT INTO area_tbl(area_id, area_name) VALUES (250010,'大津');
INSERT INTO area_tbl(area_id, area_name) VALUES (250020,'彦根');
INSERT INTO area_tbl(area_id, area_name) VALUES (260010,'京都');
INSERT INTO area_tbl(area_id, area_name) VALUES (260020,'舞鶴');
INSERT INTO area_tbl(area_id, area_name) VALUES (270000,'大阪');
INSERT INTO area_tbl(area_id, area_name) VALUES (280010,'神戸');
INSERT INTO area_tbl(area_id, area_name) VALUES (280020,'豊岡');
INSERT INTO area_tbl(area_id, area_name) VALUES (290010,'奈良');
INSERT INTO area_tbl(area_id, area_name) VALUES (290020,'風屋');
INSERT INTO area_tbl(area_id, area_name) VALUES (300010,'和歌山');
INSERT INTO area_tbl(area_id, area_name) VALUES (300020,'潮岬');
INSERT INTO area_tbl(area_id, area_name) VALUES (310010,'鳥取');
INSERT INTO area_tbl(area_id, area_name) VALUES (310020,'米子');
INSERT INTO area_tbl(area_id, area_name) VALUES (320010,'松江');
INSERT INTO area_tbl(area_id, area_name) VALUES (320020,'浜田');
INSERT INTO area_tbl(area_id, area_name) VALUES (320030,'西郷');
INSERT INTO area_tbl(area_id, area_name) VALUES (330010,'岡山');
INSERT INTO area_tbl(area_id, area_name) VALUES (330020,'津山');
INSERT INTO area_tbl(area_id, area_name) VALUES (340010,'広島');
INSERT INTO area_tbl(area_id, area_name) VALUES (340020,'庄原');
INSERT INTO area_tbl(area_id, area_name) VALUES (350010,'下関');
INSERT INTO area_tbl(area_id, area_name) VALUES (350020,'山口');
INSERT INTO area_tbl(area_id, area_name) VALUES (350030,'柳井');
INSERT INTO area_tbl(area_id, area_name) VALUES (350040,'萩');
INSERT INTO area_tbl(area_id, area_name) VALUES (360010,'徳島');
INSERT INTO area_tbl(area_id, area_name) VALUES (360020,'日和佐');
INSERT INTO area_tbl(area_id, area_name) VALUES (370000,'高松');
INSERT INTO area_tbl(area_id, area_name) VALUES (380010,'松山');
INSERT INTO area_tbl(area_id, area_name) VALUES (380020,'新居浜');
INSERT INTO area_tbl(area_id, area_name) VALUES (380030,'宇和島');
INSERT INTO area_tbl(area_id, area_name) VALUES (390010,'高知');
INSERT INTO area_tbl(area_id, area_name) VALUES (390020,'室戸岬');
INSERT INTO area_tbl(area_id, area_name) VALUES (390030,'清水');
INSERT INTO area_tbl(area_id, area_name) VALUES (400010,'福岡');
INSERT INTO area_tbl(area_id, area_name) VALUES (400020,'八幡');
INSERT INTO area_tbl(area_id, area_name) VALUES (400030,'飯塚');
INSERT INTO area_tbl(area_id, area_name) VALUES (400040,'久留米');
INSERT INTO area_tbl(area_id, area_name) VALUES (410010,'佐賀');
INSERT INTO area_tbl(area_id, area_name) VALUES (410020,'伊万里');
INSERT INTO area_tbl(area_id, area_name) VALUES (420010,'長崎');
INSERT INTO area_tbl(area_id, area_name) VALUES (420020,'佐世保');
INSERT INTO area_tbl(area_id, area_name) VALUES (420030,'厳原');
INSERT INTO area_tbl(area_id, area_name) VALUES (420040,'福江');
INSERT INTO area_tbl(area_id, area_name) VALUES (430010,'熊本');
INSERT INTO area_tbl(area_id, area_name) VALUES (430020,'阿蘇乙姫');
INSERT INTO area_tbl(area_id, area_name) VALUES (430030,'牛深');
INSERT INTO area_tbl(area_id, area_name) VALUES (430040,'人吉');
INSERT INTO area_tbl(area_id, area_name) VALUES (440010,'大分');
INSERT INTO area_tbl(area_id, area_name) VALUES (440020,'中津');
INSERT INTO area_tbl(area_id, area_name) VALUES (440030,'日田');
INSERT INTO area_tbl(area_id, area_name) VALUES (440040,'佐伯');
INSERT INTO area_tbl(area_id, area_name) VALUES (450010,'宮崎');
INSERT INTO area_tbl(area_id, area_name) VALUES (450020,'延岡');
INSERT INTO area_tbl(area_id, area_name) VALUES (450030,'都城');
INSERT INTO area_tbl(area_id, area_name) VALUES (450040,'高千穂');
INSERT INTO area_tbl(area_id, area_name) VALUES (460010,'鹿児島');
INSERT INTO area_tbl(area_id, area_name) VALUES (460020,'鹿屋');
INSERT INTO area_tbl(area_id, area_name) VALUES (460030,'種子島');
INSERT INTO area_tbl(area_id, area_name) VALUES (460040,'名瀬');
INSERT INTO area_tbl(area_id, area_name) VALUES (471010,'那覇');
INSERT INTO area_tbl(area_id, area_name) VALUES (471020,'名護');
INSERT INTO area_tbl(area_id, area_name) VALUES (471030,'久米島');
INSERT INTO area_tbl(area_id, area_name) VALUES (472000,'南大東');
INSERT INTO area_tbl(area_id, area_name) VALUES (473000,'宮古島');
INSERT INTO area_tbl(area_id, area_name) VALUES (474010,'石垣島');
INSERT INTO area_tbl(area_id, area_name) VALUES (474020,'与那国島');
登録できているかを
試しに1件抽出(select)して確認してみます。
地域マスタテーブル(area_tbl)から、地域ID="460030"の条件に一致する地域名(area_name)を取得
select area_name from area_tbl where area_id=460030;
ログ
想定通り条件に合致する地域名(area_name="種子島”)を取得できました。
このテーブルを使用して、
前回のBOTのコードを以下の通りに変更しています。
開発ソース
'use strict';
const express = require('express');
const line = require('@line/bot-sdk');
const axios = require('axios');
const PORT = process.env.PORT || 3000;
const config = {
channelAccessToken: '',
channelSecret: ''
};
const app = express();
app.post('/webhook', line.middleware(config), (req, res) => {
console.log(req.body.events);
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result));
});
const { Client } = require('pg');
const client = new line.Client(config);
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'location') {
return Promise.resolve(null);
}
let mes = ''
if(event.message.type === 'location'){
mes = '天気を確認しますね'; //メッセージだけ先に処理
getNodeVer(event.source.userId,event.message.address); //スクレイピング処理が終わったらプッシュメッセージ
}else{
//mes = event.message.text;
mes = event.message.text;
}
return client.replyMessage(event.replyToken, {
type: 'text',
text: mes
});
}
const getNodeVer = async (userId, address) => {
let city_id = '260010';
console.log("address : " + address);
// 接続
let ClientPostgres = new Client({
connectionString: 'postgres://hogehoge',
// ssl: true
ssl: { rejectUnauthorized: false }
});
await ClientPostgres.connect();
const resSQL = await ClientPostgres.query("select area_name , area_id from area_tbl");
// 地名のリストをもらう
// 従来でいえば、べた書きしていたもの
console.log(resSQL.rows);
// 検索元リスト
const searchList = resSQL.rows;
// JavaScriptで部分一致を探す
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/find
let matchData = searchList.find(function(item){
// address を 各 area_name と突き合わせて一致したものを返答する
return address.indexOf(item.area_name) > -1 // true
});
console.log("matchData");
console.log(matchData);
console.log("matchData.area_id");
console.log(matchData.area_id);
console.log("address");
console.log(address);
await ClientPostgres.end();
city_id = matchData.area_id;
const res = await axios.get('http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city_id);
const item = res.data;
await client.pushMessage(userId, {
type: 'text',
text: item.description.text,
});
}
// app.listen(8080);
app.listen(process.env.PORT || 8080);
console.log("server start! (heroku)");
LINE BOT(we-together)の動き
終わりに
- DB管理できると出来ることの幅も広がります。
- 地域コードが先頭0埋めの6桁で管理しないといけないのですが、数値で定義してしまって、登録の時には"012010"だったのに、"12010"にゼロサプレスされて、現状、旭川から宇都宮の地域では利用できません。(テーブルを更新しなおすので、少々お待ちください。)