chatbot 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chatbot.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rohit Kumar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # Chatbot
2
+
3
+ Chatbot lets you define chat commands easily.
4
+
5
+ ## Installation
6
+
7
+ Clone this repo and then run gem build and gem install, or
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'chatbot', :git => "git://github.com/ronandi/chatbot.git"
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Remember to:
18
+
19
+ require('chatbot')
20
+
21
+ ## Usage
22
+
23
+ Here's an example how to write an echo bot. It just repeats what its given:
24
+ ```ruby
25
+ require 'sinatra'
26
+ require 'chatbot'
27
+
28
+ Chatbot.configure do |config|
29
+ config.bot_id = "your_bot_id"
30
+ end
31
+
32
+ post '/' do
33
+ Chatbot.processMessage(request.body.read)
34
+ end
35
+
36
+ Chatbot.command "!echo" do |message|
37
+ puts "im here"
38
+ message
39
+ end
40
+
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chatbot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chatbot"
8
+ spec.version = ChatBot::VERSION
9
+ spec.authors = ["Rohit Kumar"]
10
+ spec.email = ["rohit.kumar@rutgers.edu"]
11
+ spec.description = "A simple gem to make writing groupme bots easier"
12
+ spec.summary = "A simple gem to make writing groupme bots easier"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "factory_girl"
24
+ end
@@ -0,0 +1,9 @@
1
+ require "chatbot/version"
2
+ require "chatbot/main"
3
+ require "chatbot/commandable"
4
+ require "chatbot/groupme_message"
5
+ require "chatbot/groupme_api"
6
+ require "chatbot/groupme_parser"
7
+ require "chatbot/configuration"
8
+
9
+ Chatbot = ChatBot::Main.new
@@ -0,0 +1,15 @@
1
+ module ChatBot
2
+ module Commandable
3
+ def is_command?
4
+ return @message[0] == '!'
5
+ end
6
+
7
+ def command
8
+ return @message.split(' ').first
9
+ end
10
+
11
+ def body
12
+ return @message.split(' ').drop(1).join(' ')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module ChatBot
2
+ class Configuration
3
+ API_URI = 'https://api.groupme.com/v3/bots/post'
4
+
5
+ attr_accessor :api_uri, :bot_id, :api_key
6
+
7
+ def initialize
8
+ @api_uri = API_URI
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ module ChatBot
6
+ class GroupMeApi
7
+ def self.send_message(message, configuration)
8
+ puts "Sending message to #{configuration.api_uri} with id: #{configuration.bot_id} and message: #{message}"
9
+ Net::HTTP.post_form(URI.parse(configuration.api_uri), { 'bot_id' => configuration.bot_id, 'text' => message })
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module ChatBot
2
+ class GroupMeMessage
3
+ include Commandable
4
+
5
+ attr_reader :message, :sender
6
+
7
+ def initialize(message, sender)
8
+ begin
9
+ sender = sender.to_str
10
+ rescue NoMethodError
11
+ raise ArgumentError, "expected sender to be string"
12
+ end
13
+ @message = message || ""
14
+ @sender = sender
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require 'json'
2
+ module ChatBot
3
+ class GroupMeParser
4
+ def self.parse(raw_message)
5
+ gm_msg = JSON.parse(raw_message)
6
+ if (gm_msg["name"].nil?)
7
+ raise ArgumentError, "Invalid GroupMe message"
8
+ end
9
+ return GroupMeMessage.new(gm_msg["text"], gm_msg["name"])
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,43 @@
1
+ module ChatBot
2
+ class Main
3
+ def initialize
4
+ @rule = {}
5
+ end
6
+ def configuration
7
+ @configuration ||= Configuration.new
8
+ end
9
+
10
+ def configure
11
+ yield(configuration) if block_given?
12
+ end
13
+
14
+ def processMessage(rawMessage)
15
+ message = GroupMeParser.parse(rawMessage)
16
+ if message.is_command?
17
+ rule = find_rule(message.command)
18
+ if rule.nil?
19
+ puts 'Message did not match a rule'
20
+ else
21
+ outmessage = rule.call(message)
22
+ unless outmessage.nil?
23
+ send_message(outmessage)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def command(command, &block)
30
+ @rule[command] = block
31
+ end
32
+
33
+ private
34
+
35
+ def find_rule(command)
36
+ return @rule[command]
37
+ end
38
+
39
+ def send_message(message)
40
+ GroupMeApi.send_message(message, configuration)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module ChatBot
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,17 @@
1
+ FactoryGirl.define do
2
+ factory :group_me_message, class: ChatBot::GroupMeMessage do
3
+ ignore do
4
+ message 'Sample message'
5
+ sender 'Sample user'
6
+ end
7
+ initialize_with { ChatBot::GroupMeMessage.new(message, sender) }
8
+ end
9
+
10
+ factory :group_me_message_with_command, class: ChatBot::GroupMeMessage do
11
+ ignore do
12
+ message '!command Sample message'
13
+ sender 'Sample user'
14
+ end
15
+ initialize_with { ChatBot::GroupMeMessage.new(message, sender) }
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ require './spec_helper'
2
+
3
+ module ChatBot
4
+ describe GroupMeMessage do
5
+ describe '.initialize' do
6
+ context 'with a hash containing a text and name' do
7
+ it 'should assign text to message, and sender to name' do
8
+ message = GroupMeMessage.new('msg','user')
9
+ message.message.should == 'msg'
10
+ message.sender.should == 'user'
11
+ end
12
+ end
13
+ context 'without string arguments' do
14
+ it 'should raise an ArgumentError' do
15
+ expect { GroupMeMessage.new(2, 3) }.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+ end
19
+ describe '.is_command' do
20
+ context 'when the message is a command' do
21
+ it 'should return true' do
22
+ message = FactoryGirl.build(:group_me_message_with_command)
23
+ message.is_command?.should == true
24
+ end
25
+ end
26
+ context 'when the message is not a command' do
27
+ it 'should return false' do
28
+ message = FactoryGirl.build(:group_me_message)
29
+ message.is_command?.should == false
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require './spec_helper'
2
+
3
+ module ChatBot
4
+ describe GroupMeParser do
5
+ describe ".parse" do
6
+ context "with valid groupme message"
7
+ it "should return a GroupMeMessage" do
8
+ msg = GroupMeParser.parse('{"name":"user", "text":"sample"}')
9
+ msg.should be_an_instance_of(GroupMeMessage)
10
+ msg.message.should eq "sample"
11
+ msg.sender.should eq "user"
12
+ end
13
+ end
14
+ context "with invalid groupme message" do
15
+ it "should raise an exception" do
16
+ expect{GroupMeParser.parse('{"something":"user", "text":"sample"}')}.to raise_error(ArgumentError)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ require '../lib/chatbot/commandable'
2
+ require '../lib/chatbot/groupme_message'
3
+ require '../lib/chatbot/groupme_parser'
4
+ require 'factory_girl'
5
+
6
+ FactoryGirl.find_definitions
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chatbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rohit Kumar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: factory_girl
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A simple gem to make writing groupme bots easier
79
+ email:
80
+ - rohit.kumar@rutgers.edu
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - chatbot.gemspec
90
+ - lib/chatbot.rb
91
+ - lib/chatbot/commandable.rb
92
+ - lib/chatbot/configuration.rb
93
+ - lib/chatbot/groupme_api.rb
94
+ - lib/chatbot/groupme_message.rb
95
+ - lib/chatbot/groupme_parser.rb
96
+ - lib/chatbot/main.rb
97
+ - lib/chatbot/version.rb
98
+ - spec/factories.rb
99
+ - spec/groupme_message.rb
100
+ - spec/groupme_parser.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.25
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: A simple gem to make writing groupme bots easier
127
+ test_files:
128
+ - spec/factories.rb
129
+ - spec/groupme_message.rb
130
+ - spec/groupme_parser.rb
131
+ - spec/spec_helper.rb