Skip to content

Commit

Permalink
add search plugin and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
gasolin committed Aug 24, 2016
1 parent fbe395a commit 6d427e5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,44 @@ bot: Hello World!

![Imgur](http://i.imgur.com/Ljjf0Fw.png)


After 0.4, Saihubot provide Search plugins, you can include via:

```html
<script defer src="saihubot-search.js"></script>
```

Check the [Search Demo](https://gasolin.github.io/saihubot/search).

### Google plugin:

```
me: g saihubot
bot: Search saihubot via Google
me: google saihubot
bot: Search saihubot via Google
me: search saihubot
bot: Search saihubot via Google
```

### Wikipedia plugin:

```
me: wiki saihubot
bot: Search saihubot via Wikipedia
me: wikipedia saihubot
bot: Search saihubot via Wikipedia
```

### Google Translate plugin

```
me: translate hello
bot: Translate hello via Google Translate
me: tr saihubot
bot: Translate hello via Google Translate
```

## Developer

### Make an plugin
Expand Down Expand Up @@ -119,6 +157,7 @@ Saihubot use MIT License

## ChangeLog

* 0.4 2016/8/24 return matched result for message instead of the origin string, add google analytics, add search plugin
* 0.3 2016/8/17 rename from HuohuBot to Saihubot, turn Saihubot to constructor, separate saihubot-dialog.js from addon
* 0.2 2016/8/6 change to robot.send method in plugin, add plugin and addon examples
* 0.1 2016/8/5 init version
35 changes: 35 additions & 0 deletions saihubot-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'strict';
document.addEventListener('DOMContentLoaded', function() {
SaihuBot.prototype.responses.push(
{ name: 'google', rule: /(^search |^google |^g* )(.*)/i, action: function(robot, msg) {
let url = 'https://www.google.com/search?q=' + encodeURIComponent(msg[2]);
let link = document.createElement('a');
link.href = url;
let linkText = document.createTextNode('Search ' + msg[2] + ' via google');
link.appendChild(linkText);
robot.sendHTML(link);
window.open(url, '_blank');
}});

SaihuBot.prototype.responses.push(
{ name: 'wikipedia', rule: /(^w |^wiki |^wikipedia )(.*)/i, action: function(robot, msg) {
let url = 'http://en.wikipedia.org/w/index.php?title=Special:Search&search=' + encodeURIComponent(msg[2]);
let link = document.createElement('a');
link.href = url;
let linkText = document.createTextNode('Search ' + msg[2] + ' via wikipedia');
link.appendChild(linkText);
robot.sendHTML(link);
window.open(url, '_blank');
}});

SaihuBot.prototype.responses.push(
{ name: 'translate', rule: /(^translate |^tr )(.*)/i, action: function(robot, msg) {
let url = 'http://translate.google.com/?q=' + encodeURIComponent(msg[2]);
let link = document.createElement('a');
link.href = url;
let linkText = document.createTextNode('Translate ' + msg[2] + ' via google translate');
link.appendChild(linkText);
robot.sendHTML(link);
window.open(url, '_blank');
}});
});
24 changes: 24 additions & 0 deletions search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="client side chatbot that can be embedded in any web site.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<title>Saihubot Search</title>
</head>
<body>
<div id="history"></div>
<input id="message"><button id="send">Send</button>

<p>
>> type `ping`, `time`, or `echo [message]` to see what's happened<br>
>> type `search [keyword]`, `g [keyword]`, `wiki [keyword]` to search<br>
>> <a href="https://github.com/gasolin/saihubot/">Download Saihubot</a>
</p>

<script defer src="saihubot.js"></script>
<script defer src="saihubot-diagnostics.js"></script>
<script defer src="saihubot-search.js"></script>
<script defer src="ga.js"></script>
</body>
</html>

0 comments on commit 6d427e5

Please sign in to comment.