botup 0.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ff5e5ebf1b3fe664afda09447d2d6222a040f7c
4
+ data.tar.gz: 2dfd7eead22e9ff7ca90b4ce105fd5b04e5d599d
5
+ SHA512:
6
+ metadata.gz: 3647400ec02cd3ff27779ac5919533fceed072b7eab9f4bcc3ed82ecbfa9b0162f587d4d4dcfa849b782d6c633fd03b46152c5f82d9fa829c3e31435018dffc5
7
+ data.tar.gz: 48b0c2aef0eef38712903aa2c5a911e023f189d70d6700c038d268c42f368180eca98c620a67000b929747149806d5db4435bfed39e5f14ee7e9bef94718b8dc
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
@@ -0,0 +1,24 @@
1
+ AllCops:
2
+ Exclude:
3
+ - '.bundle/**/*'
4
+ - 'bin/**/*'
5
+ - 'config/**/*'
6
+ - 'db/**/*'
7
+ - 'lib/tasks/**/*'
8
+ - 'app/controllers/users/confirmations_controller.rb'
9
+ - 'app/controllers/users/registrations_controller.rb'
10
+ - 'lib/extensions/simple_form/inputs/array_input.rb'
11
+ - 'vendor/assets/**/**/*'
12
+
13
+ Documentation:
14
+ Enabled: false
15
+
16
+ Metrics/LineLength:
17
+ Max: 110
18
+
19
+ Style/SymbolArray:
20
+ Enabled: true
21
+
22
+ Metrics/AbcSize:
23
+ Exclude:
24
+ - 'test/**/*'
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,50 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This Code of Conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at [INSERT EMAIL ADDRESS]. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+
45
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
46
+ version 1.3.0, available at
47
+ [http://contributor-covenant.org/version/1/3/0/][version]
48
+
49
+ [homepage]: http://contributor-covenant.org
50
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in botup.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Emil Shakirov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Botup
2
+
3
+ **Botup** is command-line tool that generates basic skeleton for telegram bot.
4
+
5
+ ## Installation
6
+
7
+ Just run this in your terminal:
8
+
9
+ $ gem install botup
10
+
11
+ ## Usage
12
+
13
+ Bootstrap new telegram bot was never so simple!
14
+
15
+ $ botup my_super_bot # Provide bot name in snake_case
16
+ $ cd my_super_bot && bundle install # Install dependencies
17
+
18
+ ## What's in the bot?
19
+
20
+ .
21
+ |____bin
22
+ | |____my_super_bot # Executable for starting your bot client
23
+ |____config
24
+ | |____config.yml # Config file storing bot token
25
+ |____Gemfile
26
+ |____lib
27
+ | |____my_super_bot
28
+ | | |____client.rb # Core telegram bot class; takes token and answer to any command sent
29
+ | | |____commands_keeper.rb # Stores all available commands (includes test command by deault);
30
+ | | |____commands_manager.rb # Takes command and handles next step
31
+ | | |____message_parser.rb # Parses sent message to command and params
32
+ | | |____messenger.rb # Handles current chat item and sends output to user
33
+ | |____my_super_bot.rb
34
+
35
+ ## How to use your bot
36
+
37
+ - Add your bot's token to **config/config.yml**
38
+ - Add desired methods to **lib/YOUR_BOT_NAME/commands_keeper.rb**. For example, to implement command */today_schedule* just create appropriate method:
39
+
40
+ ```
41
+ def today_schedule
42
+ Schedule.new(Time.now).call
43
+ end
44
+ ```
45
+ - Start bot:
46
+
47
+ ```$ my_super_bot/bin/my_super_bot```
48
+ - Try it out by sending /test command(or some other command that you have already implemented)
49
+
50
+ ![](https://monosnap.com/file/c2WA7eWNmsex9ccf9E50C36Vro17kr.png)
51
+ - Sit back and take a glass of Pina Colada!
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vaihtovirta/botup. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
+
57
+ ## Credits
58
+
59
+ Originally was inspired(and partially borrowed) by [telegram_meetup_bot](https://github.com/mendab1e/telegram_meetup_bot).
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'botup'
4
+
5
+ Botup::Generator.generate(ARGV.first)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'botup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'botup'
8
+ spec.version = Botup::VERSION
9
+ spec.authors = ['Emil Shakirov']
10
+ spec.email = ['5o.smoker@gmail.com']
11
+
12
+ spec.summary = 'Telegram bot boilerplate generator'
13
+ spec.description = 'Command-line tool for generating boilerplate for telegram bot.'
14
+ spec.homepage = 'https://github.com/vaihtovirta/botup'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ end
@@ -0,0 +1,10 @@
1
+ require 'botup/version'
2
+ require 'botup/file_processor'
3
+
4
+ module Botup
5
+ class Generator
6
+ def self.generate(name)
7
+ FileProcessor.new(name).call
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '<%= "../lib/#{snake_case_name}" %>'
4
+
5
+ <%= class_name %>.start_client
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'telegram-bot-ruby'
@@ -0,0 +1 @@
1
+ bot_token: 'YOUR_TOKEN_GOES_HERE'
@@ -0,0 +1,93 @@
1
+ require 'fileutils'
2
+ require 'erb'
3
+ require 'ostruct'
4
+
5
+ module Botup
6
+ class FileProcessor
7
+ attr_reader :base_name
8
+
9
+ def initialize(base_name)
10
+ @base_name = base_name
11
+ end
12
+
13
+ def call
14
+ prepare_files
15
+ populate_basic_templates(templates_folder, output_main_folder)
16
+ populate_bot_named_files("#{templates_folder}/core_file.erb", "#{output_lib_folder}/#{base_name}.rb")
17
+ populate_bot_named_files("#{bin_folder}/exec.erb", "#{output_folder}/bin/#{base_name}")
18
+ FileUtils.chmod('+x', "#{output_folder}/bin/#{base_name}")
19
+ end
20
+
21
+ private
22
+
23
+ def prepare_files
24
+ FileUtils.mkdir_p(output_main_folder)
25
+ FileUtils.mkdir_p("#{output_folder}/bin/")
26
+ FileUtils.mkdir_p("#{output_folder}/config/")
27
+ FileUtils.cp("#{config_folder}/config.yml", "#{output_folder}/config")
28
+ FileUtils.cp("#{config_folder}/Gemfile", output_folder)
29
+ end
30
+
31
+ def populate_basic_templates(from, to)
32
+ Dir.foreach(from) do |file_name|
33
+ current_path = "#{from}/#{file_name}"
34
+
35
+ next if file_name == '.' || file_name == '..' || file_name == 'core_file.erb'
36
+
37
+ erb = ERB.new(File.read(current_path))
38
+ result = erb.result(namespace.instance_eval { binding })
39
+
40
+ ruby_file_path = "#{to}/#{File.basename(file_name, '.erb')}.rb"
41
+
42
+ File.open(ruby_file_path, 'w') << result
43
+ end
44
+ end
45
+
46
+ def populate_bot_named_files(source, out)
47
+ erb = ERB.new(File.read(source))
48
+ result = erb.result(namespace.instance_eval { binding })
49
+ File.open("#{out}", 'w') << result
50
+ end
51
+
52
+ def bin_folder
53
+ "#{__dir__}/bin"
54
+ end
55
+
56
+ def config_folder
57
+ "#{__dir__}/config"
58
+ end
59
+
60
+ def templates_folder
61
+ "#{__dir__}/templates"
62
+ end
63
+
64
+ def core_file
65
+ "#{templates_folder}/bot.erb"
66
+ end
67
+
68
+ def output_lib_folder
69
+ "#{output_folder}/lib/"
70
+ end
71
+
72
+ def output_folder
73
+ "#{current_dir}/#{base_name}"
74
+ end
75
+
76
+ def output_main_folder
77
+ "#{output_folder}/lib/#{base_name}"
78
+ end
79
+
80
+ def current_dir
81
+ Dir.pwd
82
+ end
83
+
84
+ def namespace
85
+ OpenStruct.new(class_name: class_name,
86
+ snake_case_name: base_name)
87
+ end
88
+
89
+ def class_name
90
+ base_name.split('_').collect(&:capitalize).join
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,27 @@
1
+ module <%= class_name %>
2
+ class Client
3
+ attr_reader :token
4
+
5
+ def initialize(token)
6
+ @token = token
7
+ end
8
+
9
+ def start
10
+ begin
11
+ Telegram::Bot::Client.run(token) do |bot|
12
+ bot.listen do |message|
13
+ if message.text
14
+ messenger = Messenger.new(api: bot.api, chat_id: message.chat.id)
15
+ response = CommandsManager.new(message: message, messenger: messenger).call
16
+ messenger.send_text(response)
17
+ end
18
+ end
19
+ end
20
+ rescue Telegram::Bot::Exceptions::ResponseError => e
21
+ puts e
22
+ sleep 1
23
+ retry # run again on telegram server error
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module <%= class_name %>
2
+ module CommandsKeeper
3
+ COMMANDS = %i(test)
4
+
5
+ def test
6
+ 'Wow, I\'m using telegram bot!'
7
+ end
8
+
9
+ def help
10
+ # Put some help info here
11
+ end
12
+
13
+ private
14
+
15
+ def has_command?(command)
16
+ COMMANDS.include?(command)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'commands_keeper'
2
+
3
+ module <%= class_name %>
4
+ class CommandsManager
5
+ include CommandsKeeper
6
+
7
+ attr_reader :command, :params, :message
8
+
9
+ def initialize(args)
10
+ @message = args.fetch(:message)
11
+ @command = detect_command
12
+ @params = parser.params
13
+ end
14
+
15
+ def call
16
+ if self.class.instance_method(command).arity == 0
17
+ send command
18
+ else
19
+ send command, params
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def detect_command
26
+ has_command?(parser.command) ? parser.command : :help
27
+ end
28
+
29
+ def parser
30
+ MessageParser.new(message)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ require 'telegram/bot'
2
+ require 'yaml'
3
+
4
+ $:.unshift("#{Dir.pwd}/lib")
5
+
6
+ require '<%= snake_case_name %>/client'
7
+ require '<%= snake_case_name %>/messenger'
8
+ require '<%= snake_case_name %>/message_parser'
9
+ require '<%= snake_case_name %>/commands_keeper'
10
+ require '<%= snake_case_name %>/commands_manager'
11
+
12
+ module <%= class_name %>
13
+ CONFIGURATION = YAML.load_file("#{Dir.pwd}/config/config.yml")
14
+
15
+ def self.start_client
16
+ Client.new(CONFIGURATION['bot_token']).start
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module <%= class_name %>
2
+ class MessageParser
3
+ attr_reader :message
4
+
5
+ def initialize(message)
6
+ @message = message
7
+ end
8
+
9
+ def command
10
+ parse_message.first.to_sym
11
+ end
12
+
13
+ def params
14
+ parse_message { |words| words.drop(1) } || []
15
+ end
16
+
17
+ private
18
+
19
+ def parse_message
20
+ if message.text && message.text[0] == '/' && message.text.length > 1
21
+ message.text[1..-1].split(' ')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ module <%= class_name %>
2
+ class Messenger
3
+ attr_reader :api, :chat_id
4
+
5
+ def initialize(args)
6
+ @api = args.fetch(:api)
7
+ @chat_id = args.fetch(:chat_id)
8
+ end
9
+
10
+ def send_text(text)
11
+ api.send_message(chat_id: chat_id, text: text)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Botup
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: botup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Emil Shakirov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ description: Command-line tool for generating boilerplate for telegram bot.
28
+ email:
29
+ - 5o.smoker@gmail.com
30
+ executables:
31
+ - botup
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rubocop.yml"
37
+ - ".travis.yml"
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - bin/botup
43
+ - botup.gemspec
44
+ - lib/botup.rb
45
+ - lib/botup/bin/exec.erb
46
+ - lib/botup/config/Gemfile
47
+ - lib/botup/config/config.yml
48
+ - lib/botup/file_processor.rb
49
+ - lib/botup/templates/client.erb
50
+ - lib/botup/templates/commands_keeper.erb
51
+ - lib/botup/templates/commands_manager.erb
52
+ - lib/botup/templates/core_file.erb
53
+ - lib/botup/templates/message_parser.erb
54
+ - lib/botup/templates/messenger.erb
55
+ - lib/botup/version.rb
56
+ homepage: https://github.com/vaihtovirta/botup
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.5
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Telegram bot boilerplate generator
80
+ test_files: []
81
+ has_rdoc: