Skypeã®ãã£ããã«èªåå¿çããJavaScriptãç°¡åã«ä½ããããã«ãã
前回æ¸ãã¦ãã
ä¸æ¬ã®ããã°ã©ã ã ããèµ°ã£ã¦ãã¦ããã£ããã®ã¡ãã»ã¼ã¸ãç£è¦ããæ¡ä»¶ã«åã£ãã¡ãã»ã¼ã¸ãè¦ã¤ããã決ããããã¹ã¯ãªãããå¼ã³åºããããªã®ãä½ãããã
ã¤ã¾ããèªåå¿çã®ã³ã¢ã«ãªããããªãã¤ããã¨ã¯å®éã®å¿çãã¸ãã¯ã¯ãã©ã°ã¤ã³å¼ã«æ軽ã«æ¸ãã
Skypeのチャットに自動応答するJavaScript - 今日覚えたこと
ã¨ããã®ãä½ã£ãã
autoreply.js
ã¾ãã¯ã¡ã¤ã³ã¨ãªãã¹ã¯ãªããããã®ã¹ã¯ãªããã¯ãè¨å®ãèªã¿è¾¼ãã ãç¡éã«ã¼ãããã¦ãSkypeã®ãã£ãããç£è¦ãããããã¦ä½ãçºè¨ãåä¿¡ããããè¨å®ã«å¾ã£ã¦å¤é¨ã¹ã¯ãªãããå¼ã³åºãããã®å¦ççµæãèªåçã«çºè¨ãããã®ã
// config var conf = eval_file('autoreply.config.json'); // attach Skype var skype = new ActiveXObject('Skype4COM.Skype'); WScript.ConnectObject(skype, 'Skype_'); skype.Attach(); // main while(true){ WScript.Sleep(1000); } // event function Skype_MessageStatus(msg, status){ if(status == skype.Convert.TextToChatMessageStatus('RECEIVED')){ var user = msg.FromDisplayName; var body = msg.Body; var timestamp = msg.Timestamp; var chat = msg.Chat; WScript.echo('received: ' + [chat.FriendlyName, user, timestamp].join('::')); WScript.echo('body: ' + body); for(var i = 0; i < conf.length; i++){ var item = conf[i]; if((!item.chat || item.chat.test(chat.FriendlyName)) && (!item.user || item.user.test(user)) && (!item.body || item.body.test(body))){ WScript.echo('file: ' + item.file); var f = eval_file(item.file); var reply = f(user, body); if(!reply) continue; chat.SendMessage(reply); WScript.echo('reply: ' + reply); } } } } // eval function eval_file(filename){ var fso = new ActiveXObject('Scripting.FileSystemObject'); var path = fso.GetParentFolderName(WScript.ScriptFullName); var stream = fso.OpenTextFile(fso.BuildPath(path, filename), 1, false, -2); var t = stream.ReadAll(); var result = eval('(function(){ return (' + t + '); })();'); stream.Close(); return result; }
autoreply.config.json
è¨å®ã¯JSONã§æ¸ãã
[ { chat: /ãã£ããã¿ã¤ãã«ã®æ¡ä»¶ã®æ£è¦è¡¨ç¾(çç¥å¯)/, user: /çºè¨ããã¦ã¼ã¶IDã®æ¡ä»¶ã®æ£è¦è¡¨ç¾(çç¥å¯)/, body: /çºè¨å 容ã®æ¡ä»¶ã®æ£è¦è¡¨ç¾(çç¥å¯)/, file: 'æ¡ä»¶ã«åè´ããã¨ãã«å¼ã³åºãjsãã¡ã¤ã«.js' }, { // è¤æ°ã®æ¡ä»¶ãæå®ã§ããã } ]
ãããªæãã®æ¸å¼ãchat, user, body ã«ã¯æ£è¦è¡¨ç¾ãæå®ãã¦ããã®æ¡ä»¶ã«åè´ããçºè¨ãåä¿¡ããã¨ãã«ãfile ã«æå®ãããJavaScriptãå¼ã³åºãããã ã
æ¡ä»¶ã¯çç¥å¯è½ãªã®ã§ãfileã ããæå®ãããã¨ãå¯è½ããã®å ´åãå ¨ã¦ã®çºè¨ã«å¯¾ãã¦ã¹ã¯ãªãããå¼ã³åºãããã
ã¹ã¯ãªãã
å¼ã³åºãããã¹ã¯ãªããã¯ãç¡åé¢æ°ã®å½¢ã§æ¸ãã2ã¤ã®å¼æ°ãåããnullã¾ãã¯æååãè¿ãã
function(user, body){ if(body == 'ãã¯ãã'){ return 'ãã¯ãããããã¾ã'; }else{ return null; } }
ä¾ãã°ä¸è¨ã®ãããªæãã
ãã®ä¾ã¯ãåä¿¡ããå 容ãããã¯ãããã§ãã£ãå ´åã«ããã¯ãããããã¾ãããè¿ãã¦ãããreturnã§è¿ããæååããèªåå¿çã®å 容ã¨ãã¦çºè¨ããããã¨ã«ãªããnullãè¿ããå ´åã¯ä½ãçºè¨ãããªãã
è¦ã¯ãä½ãæååãè¿ããããªç¡åé¢æ°ãæ¸ãã°ãããããã ãã
使ãæ¹ã®ä¾ : 天æ°äºå ±
ä¾ãã°ãã天æ°ãã®æåãå«ãçºè¨ããã£ãå ´åã«ã天æ°äºå ±ãè¿ããããªèªåå¿çããã°ã©ã ã¯ã以ä¸ã®ããã«ä½ãã
autoreply.config.json
è¨å®ãã¡ã¤ã«ã¯ãããªæãã
[ { body: /天æ°/, file: 'weather.js' } ]
/天æ°/ ã®æ£è¦è¡¨ç¾ã«ãããããã¨ãã« weather.js ãå¼ã³åºãè¨å®ã
weather.js
ããã¦èå¿ã® weather.js ã¯ä»¥ä¸ã
function(user, body){ var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); var uri = 'http://weather.livedoor.com/forecast/rss/4/25.xml'; xmlhttp.open('GET', uri, false); xmlhttp.send(null); if(xmlhttp.status != 200) return null; var result = []; var items = xmlhttp.responseXML.getElementsByTagName('item'); for(var i = 0; i < items.length; i++){ var category = items[i].getElementsByTagName('category')[0].firstChild.nodeValue; var desc = items[i].getElementsByTagName('description')[0].firstChild.nodeValue; if(!/PR/.test(category)) result.push(desc); if(3 <= result.length) break; } return result.join('\n'); }
livedoorãWeather Hacksã§æä¾ãã¦ãã天æ°äºå ±ã®RSSãåå¾ãã¦ããã®ä¸ããææ°3件åã®å¤©æ°äºå ±ãè¿å´ãã¦ããã
ã¡ãªã¿ã«ãä¸è¨ã®ã³ã¼ãã¯ä»å°å¸ã®å¤©æ°ãåå¾ãã¦ãããuriã®ã¨ããã一覧ãåèã«ãã¦ä»ã®ã«å¤ãããã¨ãã§ããã
åãã
ã¾ãSkypeãèµ·åãã¦ãã°ã¤ã³ãã¦ããã
ããã¦ã
- autoreply.js
- autoreply.config.json
- weather.js
ã®3ãã¡ã¤ã«ãåãå ´æã«ç½®ãã¦ãã³ãã³ãããã³ããããã
cscript autoreply.js
ã¨ãã£ã¦ã¹ã¯ãªãããèµ·åã
ãã®Skypeã®ã¢ã«ã¦ã³ãã«å¯¾ãã¦ãå¥ãªã¢ã«ã¦ã³ããããã£ããã§ã天æ°ããå«ãè¨èã話ãã¨ãèªåå¿çã§å¤©æ°äºå ±ãè¿ã£ã¦ããã¯ãã
使ãæ¹è²ã
ã¹ã¯ãªãããä½ãã®ã¯ãå¼æ°ãæ»ãå¤ãæååãªã®ã§ç°¡åã ã¨æããSkypeã®å¶å¾¡ã«é¢ããã¨ããã¯å ¨é¨autoreply.jsã®ä¸ã§ãã£ã¦ãã®ã§æ°ã«ããªãã¦è¯ãããã ã
è¦ã¯botãªã®ã§ãé å¼µãã°äººå·¥ç¡è½ã¿ãããªã®ãä½ããã¯ãããã£ã¨ä¾¿å©ã«ãªããããªã¢ã¤ãã£ã¢ã¯ç¡ãããªã¼ã