Bluemix で、Node-RED で作成した API を、Flask から呼び出すサンプルです。
APIは、次のものを使いました。
Node-RED でメールを送る (POST編)
プログラムの構成は、
welcome.py
send_gmail.py
static/index.html
static/mail.js
です。
welcome.py
# -*- coding: utf-8 -*-
#
# welcome.py
#
# Aug/29/2017
#
# ----------------------------------------------------------------------
import sys
import os
from flask import Flask
from flask import jsonify
from flask import request
app = Flask(__name__)
from send_gmail import send_gmail_proc
# ----------------------------------------------------------------------
@app.route("/")
def welcome():
print("welcome")
return app.send_static_file('index.html')
# ----------------------------------------------------------------------
@app.route('/post_mail' ,methods=['POST'])
def mail_proc():
print("*** post_mail *** start ***")
message = []
message.append("*** mail *** start ***")
message.append("request.method = " + request.method)
#
to = request.form['to']
topic = request.form['topic']
content = request.form['content']
#
try:
send_gmail_proc(to,topic,content)
except Exception as ee:
sys.stderr.write("*** error *** send_gmail_proc ***\n")
message.append("*** error *** send_gmail_proc ***")
sys.stderr.write(str(ee) + "\n")
message.append(str(ee))
#
message.append("*** mail *** end ***")
print("*** mail *** end ***")
#
rvalue={}
rvalue['to'] = to
rvalue['content'] = content
rvalue['message'] = message
#
return jsonify(rvalue)
# ----------------------------------------------------------------------
port = os.getenv('PORT', '5000')
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=int(port))
# ----------------------------------------------------------------------
send_gmail.py
# -*- coding: utf-8 -*-
#
# send_gmail.py
#
# Aug/29/2017
# ------------------------------------------------------------------
import sys
import json
import requests
# ------------------------------------------------------------------
def send_gmail_proc(to,topic,content):
payload = {
"to": to,
"topic": topic,
"content": content
}
headers = {'content-type': 'application/json'}
url = "http://node-red-sample.mybluemix.net/post_mail"
#
rr=requests.post(url,data=json.dumps(payload),headers=headers)
#
print(rr)
print(rr.text)
# ------------------------------------------------------------------
static/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<script src="static/lib/jquery-3.2.1.min.js"></script>
<script src="static/mail.js"></script>
<title>Mail Aug/29/2017</title>
</head>
<body>
<h2>メール送信テスト</h2>
送信先のメールアドレスを入れて下さい。<p />
<input type="text" id="mail_to" style="width: 220px" /><p />
タイトルを入れて下さい。<p />
<input type="text" id="title" style="width: 400px" /><p />
メッセージを入れて下さい。<p />
<textarea id="content" rows="5" cols="80"></textarea>
<blockquote>
<button class="execute" id="send">送信</button>
</blockquote>
<div id="results">results</div><br />
<hr />
<div id="outarea_aa">outarea_aa</div>
<div id="outarea_bb">outarea_bb</div>
<div id="outarea_cc">outarea_cc</div>
<div id="outarea_dd">outarea_dd</div>
<div id="outarea_ee">outarea_ee</div>
<div id="outarea_ff">outarea_ff</div>
<div id="outarea_gg">outarea_gg</div>
<div id="outarea_hh">outarea_hh</div>
<hr />
Version: Aug/29/2017 PM 13:51<br />
</body>
</html>
static/mail.js
// -----------------------------------------------------------------------
// mail.js
//
// Aug/29/2017
//
// -----------------------------------------------------------------------
jQuery (function ()
{
jQuery("#outarea_aa").text ("*** mail.js *** 開始 ***")
send_click_monitor ()
jQuery("#outarea_hh").text ("*** mail.js *** 終了 ***")
})
// -----------------------------------------------------------------------
// [8]:
function send_click_monitor ()
{
jQuery ("button#send").click (function ()
{
jQuery ("button").css ("color","black")
jQuery ("button#" + this.id).css ("color","blue")
jQuery("#outarea_bb").text ("this.id = " + this.id)
const mail_to = jQuery("#mail_to").val ()
const topic = jQuery("#title").val ()
const content = jQuery("#content").val ()
mail_send_proc (mail_to,topic,content)
})
}
// -----------------------------------------------------------------------
// [8-4]:
function mail_send_proc (mail_to,topic,content)
{
var str_tmp = ""
str_tmp += "mail_to = " + mail_to + "<br />"
str_tmp += "topic = " + mail_to + "<br />"
str_tmp += "content = " + content + "<br />"
jQuery("#outarea_cc").html (str_tmp)
const url = "/post_mail"
var args = {
"to": mail_to,
"topic": topic,
"content": content
}
jQuery.post (url,args,function(res)
{
json_str = JSON.stringify(res)
jQuery("#outarea_ee").text (json_str)
})
}
// -----------------------------------------------------------------------