rails-bot 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +57 -0
- data/lib/rails/bot.rb +13 -1
- data/lib/rails/bot/version.rb +1 -1
- data/lib/templates/chat.coffee +38 -7
- data/lib/templates/chat_channel.rb +1 -1
- data/lib/templates/ruby_bot_job.rb +15 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9846d2501c93fa9d9703293d0120986d2f52f42
|
4
|
+
data.tar.gz: c27ae8f0655ea77fe07b7744031ea87baf75f1df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf09ba230a3836050a6ce3c473380e19175b41e9a61e7423c2cc8cfe4cd224159d8a0c2a79d8ea921d92ed230bbe29e9d9e1e8a431799813c2c67e0923a6b0de
|
7
|
+
data.tar.gz: 43a48a3f41dc8bd86488089f25602a881802e383cf2cad34a949cdca7adb5dc55af85b52f86c75d6a8f974db60df1e81a3c0c188fbaef1060791eecf6ded3bda
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Rails::Bot
|
2
2
|
Building a bot for the chatting functionality in the Rails Application. Write now using GoogleCustomSearchApi for the query part.
|
3
3
|
|
4
|
+
<a href="https://badge.fury.io/rb/rails-bot"><img src="https://badge.fury.io/rb/rails-bot.svg" alt="Gem Version" height="18"></a>
|
5
|
+
|
4
6
|
## Usage
|
5
7
|
|
6
8
|
Rails::Bot.conversation(message)
|
@@ -36,6 +38,61 @@ Or install it yourself as:
|
|
36
38
|
$ gem install rails-bot
|
37
39
|
```
|
38
40
|
|
41
|
+
Then, create the files and initializers code, make sure you have installed "gem 'google_custom_search_api'", "gem 'devise'"
|
42
|
+
|
43
|
+
```bash
|
44
|
+
rails g rails:bot:initializer
|
45
|
+
```
|
46
|
+
The above would create files for you
|
47
|
+
|
48
|
+
create config/initializers/google_initializer.rb => For the google custom seacrh API
|
49
|
+
create config/initalizers/warden_hooks.rb => For Validation at Websocket Connection
|
50
|
+
create app/views/shared/_chat.html.erb => For rendering the view for chat
|
51
|
+
create app/channels/chat_channel.rb => For communicating with the client
|
52
|
+
create app/assets/javascripts/channels/chat.coffee => For communicating with the server
|
53
|
+
create app/jobs/ruby_bot_job.rb => Bot Responds via this Active Job
|
54
|
+
|
55
|
+
In your application.html.erb, in body add this
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
<%= render "shared/chat"%>
|
59
|
+
```
|
60
|
+
|
61
|
+
Post that add these methods to your ApplicationHelper(application_helper.rb)
|
62
|
+
```ruby
|
63
|
+
def resource_name
|
64
|
+
:user
|
65
|
+
end
|
66
|
+
|
67
|
+
def resource
|
68
|
+
@resource ||= User.new
|
69
|
+
end
|
70
|
+
|
71
|
+
def devise_mapping
|
72
|
+
@devise_mapping ||= Devise.mappings[:user]
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Post that add this to your connection.rb
|
77
|
+
```ruby
|
78
|
+
identified_by :current_user
|
79
|
+
|
80
|
+
def connect
|
81
|
+
self.current_user = find_verified_user
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
def find_verified_user
|
86
|
+
if current_user = User.find_by(id: cookies.signed['user.id'])
|
87
|
+
current_user
|
88
|
+
else
|
89
|
+
reject_unauthorized_connection
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
|
39
96
|
## Contributing
|
40
97
|
Contribution directions go here.
|
41
98
|
|
data/lib/rails/bot.rb
CHANGED
@@ -10,7 +10,19 @@ module Rails
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.search message
|
13
|
-
|
13
|
+
data = {}
|
14
|
+
data["type_of_message"] = "search"
|
15
|
+
data["query"] = message
|
16
|
+
data["products"] = []
|
17
|
+
results = GoogleCustomSearchApi.search(message)
|
18
|
+
results["items"].first(5).each{|item|
|
19
|
+
every_data = {}
|
20
|
+
every_data["link"] = item["link"]
|
21
|
+
every_data["body"] = item["htmlSnippet"].first(50)
|
22
|
+
every_data["image"] = item["pagemap"]["cse_image"].first["src"]
|
23
|
+
data["products"] << every_data
|
24
|
+
}
|
25
|
+
data
|
14
26
|
end
|
15
27
|
end
|
16
28
|
end
|
data/lib/rails/bot/version.rb
CHANGED
data/lib/templates/chat.coffee
CHANGED
@@ -1,13 +1,27 @@
|
|
1
|
-
App.chatChannel = App.cable.subscriptions.create { channel: "ChatChannel", room: "
|
1
|
+
App.chatChannel = App.cable.subscriptions.create { channel: "ChatChannel", room: "Best_Room" },
|
2
2
|
received: (data) ->
|
3
3
|
@appendLine(data)
|
4
4
|
|
5
5
|
appendLine: (data) ->
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
if (data.type_of_message == "chat_person")
|
7
|
+
html = @createLine(data)
|
8
|
+
$("#room").append(html)
|
9
|
+
$('#chat-message-counter').text(parseInt($('#chat-message-counter').text()) + 1)
|
10
|
+
$('.chat-feedback').hide()
|
11
|
+
$('#room').animate({scrollTop: $('#room').prop("scrollHeight")}, 500)
|
12
|
+
else if (data.type_of_message == "bot")
|
13
|
+
html = @createLine(data)
|
14
|
+
$("#room").append(html)
|
15
|
+
$('#chat-message-counter').text(parseInt($('#chat-message-counter').text()) + 1)
|
16
|
+
$('.chat-feedback').hide()
|
17
|
+
$('#room').animate({scrollTop: $('#room').prop("scrollHeight")}, 500)
|
18
|
+
else if (data.type_of_message == "search")
|
19
|
+
html = @createCard(data)
|
20
|
+
$("#room").append(html)
|
21
|
+
$('#chat-message-counter').text(parseInt($('#chat-message-counter').text()) + 1)
|
22
|
+
$('.chat-feedback').hide()
|
23
|
+
$('#room').animate({scrollTop: $('#room').prop("scrollHeight")}, 500)
|
24
|
+
|
11
25
|
|
12
26
|
|
13
27
|
createLine: (data) ->
|
@@ -22,9 +36,25 @@ App.chatChannel = App.cable.subscriptions.create { channel: "ChatChannel", room:
|
|
22
36
|
</div> <!-- end chat-message -->
|
23
37
|
<hr>
|
24
38
|
"""
|
39
|
+
createCard: (data) ->
|
40
|
+
div_data = """
|
41
|
+
<h4>Here are results for #{data['query']}</h4><div class="chat-message clearfix" style="width:700px;overflow-x:scroll;overflow:hidden;">
|
42
|
+
"""
|
43
|
+
for data in data.products
|
44
|
+
div_data += """
|
45
|
+
<div class="card" style="width:100px;float:left;margin-right:10px;padding: 10px; border: 1px solid grey;">
|
46
|
+
<a href="#{data['link']}"><img src="#{data['image']}" alt="Avatar" style="width:100%;max-height:100px;max-width:100px;"></a>
|
47
|
+
<div class="container">
|
48
|
+
<h4><b>#{data["body"]}</b></h4>
|
49
|
+
|
50
|
+
</div>
|
51
|
+
</div>
|
52
|
+
|
53
|
+
"""
|
54
|
+
div_data += "</div>"
|
25
55
|
$(document).ready ->
|
26
56
|
$('form#message_form').submit (event) -> #HERE
|
27
|
-
App.chatChannel.send({ sent_by: $('#name').val(), body: $('#msg_text').val() })
|
57
|
+
App.chatChannel.send({ sent_by: $('#name').val(), body: $('#msg_text').val(), type_of_message: "chat_person" })
|
28
58
|
event.preventDefault()
|
29
59
|
$('#chat-message-counter').text('0')
|
30
60
|
$('.chat-feedback').show()
|
@@ -34,3 +64,4 @@ App.chatChannel = App.cable.subscriptions.create { channel: "ChatChannel", room:
|
|
34
64
|
|
35
65
|
|
36
66
|
|
67
|
+
|
@@ -1,10 +1,19 @@
|
|
1
1
|
class RubyBotJob < ApplicationJob
|
2
2
|
queue_as :default
|
3
3
|
|
4
|
-
def perform(data,room_id)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
def perform(data,room_id,to_ignore=nil)
|
5
|
+
search_keywords = %w{scouring rummage foraging forage hunting seeking hunt looking quest exploration frisking frisk activity manhunt shakedown ransacking searching help}
|
6
|
+
if (data["body"].split(' ').collect(&:downcase) & search_keywords).blank?
|
7
|
+
data["body"] = Rails::Bot.conversation(data["body"])
|
8
|
+
data["sent_by"] = "Bot"
|
9
|
+
data["time"] = Time.now.strftime("at %I:%M%p")
|
10
|
+
data["type_of_message"] = "bot"
|
11
|
+
ActionCable.server.broadcast("chat_#{room_id}", data)
|
12
|
+
else
|
13
|
+
ActionCable.server.broadcast("chat_#{room_id}", Rails::Bot.search(data["body"].gsub("Looking for","")))
|
14
|
+
end
|
9
15
|
end
|
10
|
-
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Himanshu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|