20210909のNode.jsに関する記事は2件です。

GMOあおぞらネット銀行のsunabarAPI実験場を使ってLINEのチャットボットを作ってみた

作ったもの GMOあおぞらネット銀行のAPIを利用して、Node.jsで銀行チャットボットを作ってみました。 残高照会、口座履歴紹介、振込依頼ができます。 (リアルデータではないです。GMOあおぞらネット銀行のサンドボックスsunabarAPI実験場での架空のデータです) 以前こちらのQiita記事『三菱UFJ銀行のサンプルAPIを試してみた』で書いた三菱UFJ銀行のサンプルAPIは、固定された数字しか取り出せないですが、GMOあおぞらネット銀行は、sunabarというサンドボックス環境を用意しており、仮想的に入金・振込など試せます。 なお、sunabarを利用するためにはGMOあおぞらネット銀行の口座が必要です。(口座開設は面倒な書類のやりとりは無く、ネットだけで完結します) 私の環境 macOS Catalina node.js v14.5.0 準備 API開発者ポータルでアカウント作成 https://api.gmo-aozora.com/ganb/developer/api-docs/#/STOP0101 GMOあおぞら銀行の口座開設 https://gmo-aozora.com/ 銀行口座を作成すると、 サンドボックス環境であるsunabarポータルへのログインが可能になります。 ログインID,パスワードは、リアル口座画面から確認できます。 「お客さま情報(申込・設定)」タブ  >  「開発者向け」 sunabar ポータル sunabarポータルへのログイン https://portal.sunabar.gmo-aozora.com/ こんな画面です。 自分のサンドボックス口座は、 左側をクリック すでに個人・法人の2つの口座ができてます。 ログインすると 本物のGMOあおぞら銀行のインターネットバンキングと同じUIです。 開発中に本物かsunabarか混乱したことあるほど。 なお、この画像では残高1万円ですが、デフォルトでは残高はゼロです。 では、どうやって入金するかというと、先ほどの画面の右側から入金できます。 仮想的なセブン銀行のATMという設定です。 開発 LINE LINEのチャットボットはこちらのリンク先のとおりにすれば作成できます。 1時間でLINE BOTを作るハンズオン (資料+レポート) in Node学園祭2017 APIトークン APIのトークンを入手 API開発ポータル にソースコードや仕様書があるので、それを参考に、Node.js で作成しました。 コード 同じ処理を何度も記載してるなど、もっともっと綺麗にかけると思いますが、とりあえず動いたのでヨシ!とします。 なお、今回は実用的なチャットボットを作ることが目的ではなく、APIの動作確認なので、コードがごちゃごちゃにならないように振込先・金額は固定にしてます。(手抜きではありません) server.js 'use strict'; require('date-utils'); //日付を扱うため const request = require("request"); const express = require('express'); const line = require('@line/bot-sdk'); const PORT = process.env.PORT || 3000; const config = { channelSecret: 'LINE MessagingAPIのchannelSecret', channelAccessToken: 'LINE MessagingAPIのchannelAccessToken' }; const app = express(); app.get('/', (req, res) => res.send('Hello LINE BOT!(GET)')); //ブラウザ確認用(無くても問題ない) 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 = new line.Client(config); async function handleEvent(event) { if (event.type !== 'message' || event.message.type !== 'text') { return Promise.resolve(null); } let resMessage=""; if (event.message.text == "残高"){ var options = { method: 'GET', url: 'https://api.sunabar.gmo-aozora.com/personal/v1/accounts/balances', qs: { accountId: '301010003162' }, //支店番号+口座種別(普通預金:01、当座預金:02)+口座番号 headers: { accept: 'application/json;charset=UTF-8', 'x-access-token': 'APIトークン' } }; request(options, function (error, response, body) { if (error) throw new Error(error); const data = JSON.parse(body); //console.log(data.balances[0].balance); const balance = parseInt(data.balances[0].balance); resMessage = balance.toLocaleString() + "円"; return client.replyMessage(event.replyToken, { type: 'text', text: resMessage //実際に返信の言葉を入れる箇所 }); }); }else if(event.message.text == "履歴"){ var options = { method: 'GET', url: 'https://api.sunabar.gmo-aozora.com/personal/v1/accounts/transactions', qs: { accountId: '301010003162' }, headers: { accept: 'application/json;charset=UTF-8', 'x-access-token': 'APIトークン' } }; request(options, function (error, response, body) { if (error) throw new Error(error); const data = JSON.parse(body); console.log(data); const count = data.count; if (count == 0){resMessage="本日の取引はありません"} else { resMessage = "本日の取引履歴"; for (let i = 0; i < count; i++) { const date = data.transactions[i].transactionDate; const type = data.transactions[i].transactionType; let typeStr =""; if (type==1){typeStr="入金";}else if (type ==2){typeStr="出金";} const amount = parseInt(data.transactions[i].amount); const remarks = data.transactions[i].remarks; resMessage = resMessage + "\n" + date+" "+typeStr+" "+amount.toLocaleString()+"円 "+remarks; } } return client.replyMessage(event.replyToken, { type: 'text', text: resMessage //実際に返信の言葉を入れる箇所 }); }); }else if(event.message.text == "振込"){ const date = new Date(); const currentDate = date.toFormat('YYYY-MM-DD'); //コードが複雑になるので振込先・金額は固定とした var options = { method: 'POST', url: 'https://api.sunabar.gmo-aozora.com/personal/v1/transfer/request', headers: { 'Content-Type': 'application/json', 'x-access-token': 'APIトークン', Accept: 'application/json;charset=UTF-8' }, body: { accountId: '301010003162', transferDesignatedDate: currentDate , transfers: [{ transferAmount: '1000', beneficiaryBankCode: '0398', beneficiaryBranchCode: '111', accountTypeCode: '1', accountNumber: '1234567', beneficiaryName: 'カ)アオゾラサンギョウ' }] }, json: true }; request(options, function (error, response, body) { if (error) throw new Error(error); const url = 'https://bank.sunabar.gmo-aozora.com/bank/notices/important'; resMessage = "振込受付完了ーパスワード入力してください→" + url; return client.replyMessage(event.replyToken, { type: 'text', text: resMessage //実際に返信の言葉を入れる箇所 }); }); }else { resMessage="もう1度お願いします"; return client.replyMessage(event.replyToken, { type: 'text', text: resMessage //実際に返信の言葉を入れる箇所 }); } } app.listen(PORT); console.log(`Server running at ${PORT}`); 参考サイト 既にほとんど同じことを先人の方がしてました・・・。振込のところを参考にさせてもらいました。ありがとうございました。 sunabar API実験場(GMOあおぞらネット銀行)で遊んでみました2
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

TypeScript NodeJS websocket client

install npm i websocket @types/websocket usage 共通化 const websocket = <T>(url: string, onmessage: (t: T) => void, onerror?: (e: any) => void, onclose?: () => void) => { const webClient = require("websocket").client const client: client = new webClient() client.on('connect', (con: connection) => { console.log(`connected ${con.connected}`) con.on('error', (e: any) => console.error(e)) con.on('close', () => console.log("websockets closed.")) con.on('message', (message: Message) => { if (message.type === "utf8") { const t = JSON.parse(message.utf8Data) as T onmessage(t) } }) if (onerror) { con.on('error', onerror) } if (onclose) { con.on('close', onclose) } }) client.connect(url) } 利用例 この例では、仮想通貨取引所binanceへbtcusdt, xrpusdtの約定データをサブスクライブしている。 interface AggTrade { e: string E: number a: number s: string p: string q: string f: number l: number T: number m: boolean } function test(){ websocket<AggTrade>("wss://fstream.binance.com/ws/" + "btcusdt@aggTrade", t => console.log(`Pare:${t.s}`)) websocket<AggTrade>("wss://fstream.binance.com/ws/" + "xrpusdt@aggTrade", t => console.log(`Pare:${t.s}`)) return } test() 実行結果 Pare:BTCUSDT Pare:BTCUSDT Pare:BTCUSDT Pare:XRPUSDT Pare:BTCUSDT Pare:BTCUSDT Pare:BTCUSDT Pare:BTCUSDT Pare:BTCUSDT Pare:XRPUSDT Pare:XRPUSDT Pare:XRPUSDT Pare:BTCUSDT Document
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む