なんかGASで作りたいと思いつくりました。
##ソースコード
var ACCESS_TOKEN = "LINEのアクセストークン";
var url = 'https://api.line.me/v2/bot/message/reply';
function doPost(e) {
// WebHookで受信した応答用Token
var replyToken = JSON.parse(e.postData.contents).events[0].replyToken;
// ユーザーのメッセージを取得
var userMessage = JSON.parse(e.postData.contents).events[0].message.text;
// 応答メッセージ用のAPI URL
//
var sand_text=siritori(userMessage);
UrlFetchApp.fetch(url, {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + ACCESS_TOKEN,
},
'method': 'post',
'payload': JSON.stringify({
'replyToken':replyToken,
'messages': [{
'type': 'text',
'text': sand_text+"\nhttps://ja.wikipedia.org/wiki/"+sand_text,
}],
}),
});
return ContentService.createTextOutput(JSON.stringify({ 'content': 'post ok' })).setMimeType(ContentService.MimeType.JSON);
}
function siritori(user_msg) {
//語尾に「ん」が付く漢字
var kanzi=["論","缶","天","点","覧","案","暗","全","員","印","院","因","引","飲","運","温","円","縁","園"
,"延","塩","遠","音","恩","韓","艦","金","菌","禁","筋","君","勲","訓","県","兼","券","件","剣","健","圏"
,"紺","産","酸","山","算","新","臣","癌","玩","寸","損","村","短","痰","担","沈","陳","賃","典","品","貧"
,"分","糞","墳","粉","編","辺","本","南","認","燃","粘","万","満","民","眠","面","麺","綿","紋","悶","四"
,"欄","乱","卵","覧","濫","林","倫","麟","錬","連","練","恋","湾","椀","腕"];
var words=[];
var name=XmlService.getNamespace("http://btonic.est.co.jp/NetDic/NetDicV09");
var return_msg = "";
res =UrlFetchApp.fetch("http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite?Dic=wpedia&Word="+str_chenge(user_msg,'katakana').slice(-1)+"&Scope=HEADWORD&Match=STARTWITH&Merge=AND&Prof=XHTML&PageSize=200&PageIndex=0")
var xmldocs = XmlService.parse(res.getContentText());
var items =xmldocs.getRootElement().getChild("TitleList",name).getChildren("DicItemTitle",name);
for (var index = 0; index < items.length; index++) {
var word= items[index].getChild("Title",name).getChildText("span",XmlService.getNamespace(""));
word= word.replace(/ *\([^)]*\) */g, "");
if (word.slice(-1)!="ん"&&word.slice(-1)!="ン"&&kanzi.indexOf(word.slice(-1))==-1) {
words.push(word);
}
}
//ひらがな
res =UrlFetchApp.fetch("http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite?Dic=wpedia&Word="+str_chenge(user_msg,'hiragana').slice(-1)+"&Scope=HEADWORD&Match=STARTWITH&Merge=AND&Prof=XHTML&PageSize=200&PageIndex=0")
xmldocs = XmlService.parse(res.getContentText());
items =xmldocs.getRootElement().getChild("TitleList",name).getChildren("DicItemTitle",name);
for (var index2 = 0; index2 < items.length; index2++) {
var word= items[index2].getChild("Title",name).getChildText("span",XmlService.getNamespace(""));
word= word.replace(/ *\([^)]*\) */g, "");
if (word.slice(-1)!="ン"&&word.slice(-1)!="ん"&&kanzi.indexOf(word.slice(-1))==-1) {
words.push(word);
}
}
return_msg=words[Math.floor( Math.random() * words.length )];
doclog(user_msg+"==>"+return_msg);
return return_msg;
}
function str_chenge(str,opt) {
var r= UrlFetchApp.fetch("https://labs.goo.ne.jp/api/hiragana", {
'headers': {
'Content-Type': "application/json",
},
'method': 'post',
'payload': JSON.stringify({
'app_id': '下に書く',
'sentence': str,
'output_type': opt
}),
});
return JSON.parse(r).converted;
}
LINEのアクセストークンは一行目を書き換えてください。
このスクリプトではgooラボのひらがな化APIを使ってるので
gooラボAPI利用登録からgithubで登録してappidを取得して、str_chenge()のapp_idを書き換えてください。
function str_chenge(str,opt) {
var r= UrlFetchApp.fetch("https://labs.goo.ne.jp/api/hiragana", {
'headers': {
'Content-Type': "application/json",
},
'method': 'post',
'payload': JSON.stringify({
'app_id': '',// ←ここを書き換え
'sentence': str,
'output_type': opt
}),
});
return JSON.parse(r).converted;
}
最後が「ん」になる漢字はkanzi
に追加しているのでカスタマイズしてください。
わからないことはコメントで知らせてください。