botup 0.3.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c603c0ddf02ac224105e1b3a0f9749ba8ed118b
4
- data.tar.gz: 84b3c8721dd37268f33cf0e04e176e6ae7539e79
3
+ metadata.gz: 393408836b80c655ca0281bbba7c8d25ddce2232
4
+ data.tar.gz: 14ce81b6bae79b350b0593b4f9651c318ce4cdc9
5
5
  SHA512:
6
- metadata.gz: 1a322d7667d52598815b3b3c4eb51cfee014d853a9c3d5eec100363fbbb882aa694a3e236c34c28f2cc68c9358ad5d2ece2604168c7e644276248d10725f89ad
7
- data.tar.gz: 8616548bbd8b66761bb906cff83a0118d4336ed07dd37e73ac4738a2d83e90baef2c40e5b281b1e78383424fe0c1bb30ee623eb1113b4d0852a5778aa17f654c
6
+ metadata.gz: 7f87ae0e9eb0b1e170f6fe12ef8537c62783ec6e517dedab54d938390b38b189c718c58cd9e6accef4ca6c35b3bd727fe4287974247b3513a31c60c587963543
7
+ data.tar.gz: abc5c181956d9ecf7a918bec00fa17d0207ea2d51e596a8366b78f3c7e58b97a9c3c3a4229fea957fd5fab48a6a965e1803663118bf08155ff5789ad3d9e7313
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /.idea
11
+ Gemfile.lock
@@ -2,13 +2,6 @@ AllCops:
2
2
  Exclude:
3
3
  - '.bundle/**/*'
4
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
5
 
13
6
  Documentation:
14
7
  Enabled: false
data/README.md CHANGED
@@ -14,41 +14,34 @@ Bootstrap new telegram bot was never so simple!
14
14
 
15
15
  $ botup my_super_bot # Provide bot name in snake_case
16
16
  $ cd my_super_bot && bundle install # Install dependencies
17
-
17
+
18
18
  ## What's in the bot?
19
19
 
20
20
  .
21
21
  |____bin
22
22
  | |____my_super_bot # Executable for starting your bot client
23
- |____config
24
- | |____config.yml # Config file storing bot token
25
- |____Gemfile
23
+ |____Gemfile
26
24
  |____lib
27
25
  | |____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
26
+ | | |____client.rb # Telegram client which listens to incomming messages
27
+ | | |____commands_keeper.rb # Stores all available commands (includes test command by deault);
28
+ | | |____commands_manager.rb # Routes command to the appropriate one from commands_keeper.rb
31
29
  | | |____message_parser.rb # Parses sent message to command and params
32
- | | |____messenger.rb # Handles current chat item and sends output to user
33
30
  | |____my_super_bot.rb
34
-
35
- ## How to use your bot
36
31
 
37
- - Add your bot's token to **config/config.yml**
32
+ ## How to use your bot
33
+ - Obtain bot api key by @BotFather and put it in *.env* file.
38
34
  - Add desired methods to **lib/YOUR_BOT_NAME/commands_keeper.rb**. For example, to implement command */today_schedule* just create appropriate method:
39
35
 
40
- ```
41
- def today_schedule
42
- Schedule.new(Time.now).call
36
+ ```ruby
37
+ def friday
38
+ Time.now.friday? ? "Hooray it's friday" : "It's always Friday somewhere."
43
39
  end
44
40
  ```
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)
41
+ - Start bot by running executable:
49
42
 
50
- ![](https://monosnap.com/file/c2WA7eWNmsex9ccf9E50C36Vro17kr.png)
51
- - Sit back and take a glass of Pina Colada!
43
+ ```$ my_super_bot/bin/my_super_bot```
44
+ - Try it out by sending /friday command(or some other command that you have already implemented)
52
45
 
53
46
  ## Contributing
54
47
 
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'botup/version'
@@ -19,5 +18,5 @@ Gem::Specification.new do |spec|
19
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
19
  spec.require_paths = ['lib']
21
20
 
22
- spec.add_development_dependency 'bundler', '~> 1.10'
21
+ spec.add_development_dependency 'bundler', '~> 1.16'
23
22
  end
@@ -6,4 +6,4 @@ Bundler.setup
6
6
 
7
7
  require '<%= "./lib/#{snake_case_name}" %>'
8
8
 
9
- <%= class_name %>.start_client
9
+ <%= class_name %>.start
@@ -0,0 +1 @@
1
+ TELEGRAM_BOT_TOKEN=GET_A_TOKEN
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'dotenv'
3
4
  gem 'telegram-bot-ruby'
@@ -4,6 +4,8 @@ require 'ostruct'
4
4
 
5
5
  module Botup
6
6
  class FileProcessor
7
+ FILE_NAMES_TO_SKIP = ['.', '..', 'core_file.erb'].freeze
8
+
7
9
  attr_reader :base_name
8
10
 
9
11
  def initialize(base_name)
@@ -23,16 +25,15 @@ module Botup
23
25
  def prepare_files
24
26
  FileUtils.mkdir_p(output_main_folder)
25
27
  FileUtils.mkdir_p("#{output_folder}/bin/")
26
- FileUtils.mkdir_p("#{output_folder}/config/")
27
- FileUtils.cp("#{config_folder}/config.yml", "#{output_folder}/config")
28
28
  FileUtils.cp("#{config_folder}/Gemfile", output_folder)
29
+ FileUtils.cp("#{config_folder}/.env", output_folder)
29
30
  end
30
31
 
31
32
  def populate_basic_templates(from, to)
32
33
  Dir.foreach(from) do |file_name|
33
34
  current_path = "#{from}/#{file_name}"
34
35
 
35
- next if file_name == '.' || file_name == '..' || file_name == 'core_file.erb'
36
+ next if FILE_NAMES_TO_SKIP.include?(file_name)
36
37
 
37
38
  erb = ERB.new(File.read(current_path))
38
39
  result = erb.result(namespace.instance_eval { binding })
@@ -43,10 +44,10 @@ module Botup
43
44
  end
44
45
  end
45
46
 
46
- def populate_bot_named_files(source, out)
47
+ def populate_bot_named_files(source, output)
47
48
  erb = ERB.new(File.read(source))
48
49
  result = erb.result(namespace.instance_eval { binding })
49
- File.open("#{out}", 'w') << result
50
+ File.open(output.to_s, 'w') << result
50
51
  end
51
52
 
52
53
  def bin_folder
@@ -1,7 +1,5 @@
1
1
  module <%= class_name %>
2
2
  class Client
3
- attr_reader :token
4
-
5
3
  def initialize(token)
6
4
  @token = token
7
5
  end
@@ -18,22 +16,16 @@ module <%= class_name %>
18
16
 
19
17
  private
20
18
 
21
- def run_client(&block)
22
- Telegram::Bot::Client.run(token) do |bot|
23
- yield bot if block_given?
24
- end
25
- end
19
+ attr_reader :token
26
20
 
27
21
  def respond
28
- run_client do |bot|
22
+ Telegram::Bot::Client.run(token) do |bot|
29
23
  bot.listen do |message|
30
- Responder.new(bot.api, message).call if message.text
24
+ next unless message.text
25
+
26
+ Responder.new(api: bot.api, message: message).call
31
27
  end
32
28
  end
33
29
  end
34
-
35
- def get_updates(bot, next_offset)
36
- bot.api.getUpdates(offset: next_offset, timeout: 20)
37
- end
38
30
  end
39
31
  end
@@ -1,19 +1,11 @@
1
1
  module <%= class_name %>
2
2
  module CommandsKeeper
3
- COMMANDS = %i(test)
4
-
5
- def test
3
+ def test(params:, message:)
6
4
  'Wow, I\'m using telegram bot!'
7
5
  end
8
6
 
9
- def help
10
- # Put some help info here
11
- end
12
-
13
- private
14
-
15
- def has_command?(command)
16
- COMMANDS.include?(command)
7
+ def help(**)
8
+ "Put some help information here"
17
9
  end
18
10
  end
19
11
  end
@@ -1,31 +1,18 @@
1
1
  module <%= class_name %>
2
2
  class CommandsManager
3
3
  include CommandsKeeper
4
+ extend Forwardable
4
5
 
5
- attr_reader :command, :params, :message
6
+ def_delegators :@parser, :message, :command, :params
7
+ attr_reader :parser
6
8
 
7
- def initialize(args)
8
- @message = args.fetch(:message)
9
- @command = detect_command
10
- @params = parser.params
11
- end
12
-
13
- def call
14
- if self.class.instance_method(command).arity == 0
15
- send command
16
- else
17
- send command, params
18
- end
19
- end
20
-
21
- private
22
9
 
23
- def detect_command
24
- has_command?(parser.command) ? parser.command : :help
10
+ def initialize(parser:)
11
+ @parser = parser
25
12
  end
26
13
 
27
- def parser
28
- MessageParser.new(message)
14
+ def call
15
+ send(command, params: params, message: message)
29
16
  end
30
17
  end
31
18
  end
@@ -1,18 +1,16 @@
1
+ require 'dotenv/load'
1
2
  require 'telegram/bot'
2
- require 'yaml'
3
3
 
4
- $:.unshift("#{Dir.pwd}/lib")
4
+ $LOAD_PATH.unshift("#{Dir.pwd}/lib")
5
5
 
6
- require '<%= snake_case_name %>/configurator'
7
6
  require '<%= snake_case_name %>/client'
8
- require '<%= snake_case_name %>/messenger'
9
7
  require '<%= snake_case_name %>/message_parser'
10
8
  require '<%= snake_case_name %>/commands_keeper'
11
9
  require '<%= snake_case_name %>/commands_manager'
12
10
  require '<%= snake_case_name %>/responder'
13
11
 
14
12
  module <%= class_name %>
15
- def self.start_client
16
- Client.new(Configurator.bot_token).start
17
- end
13
+ def self.start
14
+ Client.new(ENV['TELEGRAM_BOT_TOKEN']).start
15
+ end
18
16
  end
@@ -1,33 +1,40 @@
1
1
  module <%= class_name %>
2
2
  class MessageParser
3
- attr_reader :message
3
+ DEFAULT_COMMAND = :help.freeze
4
+ COMMANDS = %i[test].freeze
4
5
 
5
- def initialize(message)
6
+ attr_reader :message, :commands
7
+
8
+ def initialize(message:)
6
9
  @message = message
7
10
  end
8
11
 
9
12
  def command
10
- parse_message.first.to_sym
13
+ COMMANDS.find { |command| command == parsed_message&.first&.to_sym } || DEFAULT_COMMAND
11
14
  end
12
15
 
13
16
  def params
14
- parse_message { |words| words.drop(1) } || []
17
+ parsed_message&.drop(1) || []
15
18
  end
16
19
 
17
20
  private
18
21
 
19
- def parse_message
20
- if message_present? && message_starts_with_slash?
21
- message.text[1..-1].split(' ')
22
- end
22
+ def can_parse?
23
+ message_present? && message_starts_with_slash?
23
24
  end
24
25
 
25
26
  def message_present?
26
- message.text && message.text.length > 1
27
+ message.text&.length&.positive?
27
28
  end
28
29
 
29
30
  def message_starts_with_slash?
30
- message.text[0] == '/'
31
+ message.text.start_with?("/")
32
+ end
33
+
34
+ def parsed_message
35
+ return unless can_parse?
36
+
37
+ message.text[1..-1].split(" ")
31
38
  end
32
39
  end
33
40
  end
@@ -1,23 +1,18 @@
1
1
  module <%= class_name %>
2
2
  class Responder
3
- attr_reader :api, :message
3
+ attr_reader :api, :message, :response
4
+ private :api, :message, :response
4
5
 
5
- def initialize(api, message)
6
- @api, @message = api, message
7
- end
8
-
9
- def call
10
- messenger.send_text(parsed_response)
11
- end
12
-
13
- private
6
+ def initialize(api:, message:)
7
+ @api = api
8
+ @message = message
14
9
 
15
- def messenger
16
- @messenger ||= Messenger.new(api: api, chat_id: message.chat.id)
10
+ parser = MessageParser.new(message: message)
11
+ @response = CommandsManager.new(parser: parser).call
17
12
  end
18
13
 
19
- def parsed_response
20
- @parsed_response ||= CommandsManager.new(message: message, messenger: messenger).call
14
+ def call
15
+ api.send_message(chat_id: message.chat.id, text: response)
21
16
  end
22
17
  end
23
18
  end
@@ -1,3 +1,3 @@
1
1
  module Botup
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: botup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Shakirov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-13 00:00:00.000000000 Z
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '1.16'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '1.16'
27
27
  description: Command-line tool for generating boilerplate for telegram bot.
28
28
  email:
29
29
  - 5o.smoker@gmail.com
@@ -43,16 +43,14 @@ files:
43
43
  - botup.gemspec
44
44
  - lib/botup.rb
45
45
  - lib/botup/bin/exec.erb
46
+ - lib/botup/config/.env
46
47
  - lib/botup/config/Gemfile
47
- - lib/botup/config/config.yml
48
48
  - lib/botup/file_processor.rb
49
49
  - lib/botup/templates/client.erb
50
50
  - lib/botup/templates/commands_keeper.erb
51
51
  - lib/botup/templates/commands_manager.erb
52
- - lib/botup/templates/configurator.erb
53
52
  - lib/botup/templates/core_file.erb
54
53
  - lib/botup/templates/message_parser.erb
55
- - lib/botup/templates/messenger.erb
56
54
  - lib/botup/templates/responder.erb
57
55
  - lib/botup/version.rb
58
56
  homepage: https://github.com/vaihtovirta/botup
@@ -75,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
73
  version: '0'
76
74
  requirements: []
77
75
  rubyforge_project:
78
- rubygems_version: 2.5.1
76
+ rubygems_version: 2.6.13
79
77
  signing_key:
80
78
  specification_version: 4
81
79
  summary: Telegram bot boilerplate generator
@@ -1 +0,0 @@
1
- bot_token: <%= ENV['YOUR_TELEGRAM_TOKEN'] %>
@@ -1,23 +0,0 @@
1
- module <%= class_name %>
2
- module Configurator
3
- CONFIG_PATH = 'config/config.yml'.freeze
4
-
5
- module_function
6
-
7
- def bot_token
8
- loaded['bot_token']
9
- end
10
-
11
- def loaded
12
- YAML.load(erb_result)
13
- end
14
-
15
- def yaml_file
16
- File.read(CONFIG_PATH)
17
- end
18
-
19
- def erb_result
20
- ERB.new(yaml_file).result
21
- end
22
- end
23
- end
@@ -1,14 +0,0 @@
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