7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GASでしりとりができるLINE BOTを作る

Posted at

なんか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に追加しているのでカスタマイズしてください。

わからないことはコメントで知らせてください。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?