kogno 1.0.1
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 +7 -0
 - data/bin/kogno +92 -0
 - data/lib/boot.rb +9 -0
 - data/lib/core/bin_helpers/messenger_ctl.rb +48 -0
 - data/lib/core/bin_helpers/scaffolding.rb +203 -0
 - data/lib/core/bin_helpers/scheduled_messages_ctl.rb +95 -0
 - data/lib/core/bin_helpers/sequences_ctl.rb +95 -0
 - data/lib/core/bin_helpers/server_ctl.rb +127 -0
 - data/lib/core/bin_helpers/telegram_ctl.rb +48 -0
 - data/lib/core/bin_helpers/webhook_ctl.rb +96 -0
 - data/lib/core/db.rb +8 -0
 - data/lib/core/extensions/array.rb +28 -0
 - data/lib/core/extensions/hash.rb +12 -0
 - data/lib/core/extensions/logger.rb +70 -0
 - data/lib/core/extensions/string.rb +6 -0
 - data/lib/core/extensions/wit.rb +60 -0
 - data/lib/core/global_methods.rb +67 -0
 - data/lib/core/helpers/string.rb +105 -0
 - data/lib/core/lib/base_config.rb +17 -0
 - data/lib/core/lib/block_params.rb +4 -0
 - data/lib/core/lib/context.rb +1573 -0
 - data/lib/core/lib/error_handler.rb +36 -0
 - data/lib/core/lib/message.rb +182 -0
 - data/lib/core/lib/messenger/api.rb +281 -0
 - data/lib/core/lib/messenger/facebook_graph.rb +32 -0
 - data/lib/core/lib/messenger/message.rb +202 -0
 - data/lib/core/lib/messenger/notification.rb +351 -0
 - data/lib/core/lib/messenger/post_comment.rb +104 -0
 - data/lib/core/lib/messenger/recurring_notification.rb +81 -0
 - data/lib/core/lib/nlp.rb +191 -0
 - data/lib/core/lib/notification.rb +371 -0
 - data/lib/core/lib/spelling.rb +13 -0
 - data/lib/core/lib/telegram/api.rb +197 -0
 - data/lib/core/lib/telegram/chat_activity.rb +111 -0
 - data/lib/core/lib/telegram/inline_query.rb +112 -0
 - data/lib/core/lib/telegram/message.rb +327 -0
 - data/lib/core/lib/telegram/notification.rb +507 -0
 - data/lib/core/lib/whatsapp/api.rb +153 -0
 - data/lib/core/lib/whatsapp/message.rb +132 -0
 - data/lib/core/lib/whatsapp/notification.rb +206 -0
 - data/lib/core/lib/whatsapp/status_message.rb +58 -0
 - data/lib/core/loaders/config_files.rb +15 -0
 - data/lib/core/models/chat_log.rb +4 -0
 - data/lib/core/models/long_payload.rb +25 -0
 - data/lib/core/models/matched_message.rb +5 -0
 - data/lib/core/models/messenger_recurring_notification.rb +16 -0
 - data/lib/core/models/scheduled_message.rb +40 -0
 - data/lib/core/models/sequence.rb +29 -0
 - data/lib/core/models/telegram_chat_group.rb +26 -0
 - data/lib/core/models/user.rb +285 -0
 - data/lib/core/web/webhook.rb +198 -0
 - data/lib/kogno.rb +130 -0
 - data/scaffolding/new_project/Gemfile +3 -0
 - data/scaffolding/new_project/application.rb +5 -0
 - data/scaffolding/new_project/bot/contexts/main_context.rb +10 -0
 - data/scaffolding/new_project/bot/conversation.rb +14 -0
 - data/scaffolding/new_project/bot/models/user.rb +3 -0
 - data/scaffolding/new_project/config/application.rb +28 -0
 - data/scaffolding/new_project/config/database.yml +8 -0
 - data/scaffolding/new_project/config/locales/en.yml +4 -0
 - data/scaffolding/new_project/config/locales/es.yml +3 -0
 - data/scaffolding/new_project/config/nlp.rb +23 -0
 - data/scaffolding/new_project/config/platforms/messenger.rb +74 -0
 - data/scaffolding/new_project/config/platforms/telegram.rb +45 -0
 - data/scaffolding/new_project/config/platforms/whatsapp.rb +13 -0
 - data/scaffolding/new_project/web/routes.rb +10 -0
 - metadata +220 -0
 
    
        checksums.yaml
    ADDED
    
    | 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ---
         
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 1cc825f1588ff8f42e48f3c66179b48f19cd8a4870b83873ffabd4113b397eb1
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 840b0e50ecfe3a4a29d3ba1a14a091088eb8c9e5702c55f06fbb3cb6966bb03f
         
     | 
| 
      
 5 
     | 
    
         
            +
            SHA512:
         
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: cfe719306daaa60adeae5f3371fe6152acfba284121bce9ddc61bb69cfc44833388aa642d6fb9cb50f7af50b0e3a92587fe17b9e286f30179aafbf5ada38530e
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: fdd5ba07dc827464e9a564fdf4ff5d5a389dd4c3acd365880eeb0449bdad285a5f0118458c68c8774d32bea3cf29d68c3fda462329460f7fe58297e05e64659f
         
     | 
    
        data/bin/kogno
    ADDED
    
    | 
         @@ -0,0 +1,92 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env ruby -W0
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'kogno'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'core/loaders/config_files'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            require 'core/bin_helpers/webhook_ctl'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'core/bin_helpers/sequences_ctl'
         
     | 
| 
      
 7 
     | 
    
         
            +
            require 'core/bin_helpers/scheduled_messages_ctl'
         
     | 
| 
      
 8 
     | 
    
         
            +
            require 'core/bin_helpers/messenger_ctl'
         
     | 
| 
      
 9 
     | 
    
         
            +
            require 'core/bin_helpers/telegram_ctl'
         
     | 
| 
      
 10 
     | 
    
         
            +
            require 'core/bin_helpers/server_ctl'
         
     | 
| 
      
 11 
     | 
    
         
            +
            require 'core/bin_helpers/scaffolding'
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            require 'core/lib/messenger/api'
         
     | 
| 
      
 14 
     | 
    
         
            +
            require 'core/lib/telegram/api'
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            command = (ARGV[0].to_sym rescue nil)
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            # Kogno::Application.create_project_folders
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            help_message = %{Usage:
         
     | 
| 
      
 21 
     | 
    
         
            +
              kogno 
         
     | 
| 
      
 22 
     | 
    
         
            +
                  start|stop|restart
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  ## Control daemons separatelly
         
     | 
| 
      
 25 
     | 
    
         
            +
                  http start|stop|restart|status|fg
         
     | 
| 
      
 26 
     | 
    
         
            +
                  sequences start|stop|restart|fg
         
     | 
| 
      
 27 
     | 
    
         
            +
                  scheduled_messages start|stop|restart|fg
         
     | 
| 
      
 28 
     | 
    
         
            +
                  
         
     | 
| 
      
 29 
     | 
    
         
            +
                  runner \[ruby code\]
         
     | 
| 
      
 30 
     | 
    
         
            +
                  new \[proyect name\] <- Create a new chatbot
         
     | 
| 
      
 31 
     | 
    
         
            +
                  install <- Create sql tables needed
         
     | 
| 
      
 32 
     | 
    
         
            +
                  messenger
         
     | 
| 
      
 33 
     | 
    
         
            +
                      menu on|off
         
     | 
| 
      
 34 
     | 
    
         
            +
                      get_started on|off \[page_id\]
         
     | 
| 
      
 35 
     | 
    
         
            +
                      greeting on|off
         
     | 
| 
      
 36 
     | 
    
         
            +
                      update_whitelisted_domains
         
     | 
| 
      
 37 
     | 
    
         
            +
                      ice_breakers on|off
         
     | 
| 
      
 38 
     | 
    
         
            +
                  telegram
         
     | 
| 
      
 39 
     | 
    
         
            +
                      webhook start|stop
         
     | 
| 
      
 40 
     | 
    
         
            +
                      set_commands #{Kogno::Telegram::Api.command_valid_scopes.join("|")}|all
         
     | 
| 
      
 41 
     | 
    
         
            +
                      delete_commands #{Kogno::Telegram::Api.command_valid_scopes.join("|")}
         
     | 
| 
      
 42 
     | 
    
         
            +
            }
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            case command
         
     | 
| 
      
 45 
     | 
    
         
            +
              when :http
         
     | 
| 
      
 46 
     | 
    
         
            +
                Kogno::WebhookCtl.options(ARGV[1])
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
              when :start, :stop, :restart, :status
         
     | 
| 
      
 49 
     | 
    
         
            +
                Kogno::ServerCtl.options(ARGV[0])  
         
     | 
| 
      
 50 
     | 
    
         
            +
                
         
     | 
| 
      
 51 
     | 
    
         
            +
              when :console, :c  
         
     | 
| 
      
 52 
     | 
    
         
            +
                require 'irb'
         
     | 
| 
      
 53 
     | 
    
         
            +
                require 'irb/completion'    
         
     | 
| 
      
 54 
     | 
    
         
            +
                require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 55 
     | 
    
         
            +
                logger.write "Loading #{Kogno::Application.config.environment} environment (Kogno #{Gem.loaded_specs["kogno"].version.version})", :bright
         
     | 
| 
      
 56 
     | 
    
         
            +
                ARGV.clear
         
     | 
| 
      
 57 
     | 
    
         
            +
                IRB.start 
         
     | 
| 
      
 58 
     | 
    
         
            +
                IRB.conf[:DEBUG_LEVEL]=0
         
     | 
| 
      
 59 
     | 
    
         
            +
                IRB.conf[:PROMPT_MODE] = :MY_PROMPT
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
              when :sequences, :seq
         
     | 
| 
      
 62 
     | 
    
         
            +
                Kogno::SequencesCtl.options(ARGV[1])
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
              when :scheduled_messages, :sm
         
     | 
| 
      
 65 
     | 
    
         
            +
                Kogno::ScheduledMessagesCtl.options(ARGV[1])
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
              when :runner
         
     | 
| 
      
 68 
     | 
    
         
            +
                require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 69 
     | 
    
         
            +
                eval(ARGV[1])
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
              when :new
         
     | 
| 
      
 72 
     | 
    
         
            +
                Kogno::Scaffolding.new_project
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
              when :install
         
     | 
| 
      
 75 
     | 
    
         
            +
                Kogno::Scaffolding.install
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
              when :messenger
         
     | 
| 
      
 78 
     | 
    
         
            +
                Kogno::MessengerCtl.options(ARGV)
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
      
 80 
     | 
    
         
            +
              when :telegram
         
     | 
| 
      
 81 
     | 
    
         
            +
                Kogno::TelegramCtl.options(ARGV)
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
              when :help
         
     | 
| 
      
 84 
     | 
    
         
            +
                puts help_message
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
              when :version
         
     | 
| 
      
 87 
     | 
    
         
            +
                puts Gem.loaded_specs["kogno"].version.version
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
              else
         
     | 
| 
      
 90 
     | 
    
         
            +
                puts help_message
         
     | 
| 
      
 91 
     | 
    
         
            +
                
         
     | 
| 
      
 92 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/boot.rb
    ADDED
    
    
| 
         @@ -0,0 +1,48 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Kogno
         
     | 
| 
      
 2 
     | 
    
         
            +
            class MessengerCtl
         
     | 
| 
      
 3 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def options(a)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  page_id = a[3] rescue nil
         
     | 
| 
      
 7 
     | 
    
         
            +
                  case (a[1].to_sym rescue nil)
         
     | 
| 
      
 8 
     | 
    
         
            +
                    when :menu
         
     | 
| 
      
 9 
     | 
    
         
            +
                      if a[2] == 'on'
         
     | 
| 
      
 10 
     | 
    
         
            +
                        Kogno::Messenger::Api.persistent_menu(page_id)
         
     | 
| 
      
 11 
     | 
    
         
            +
                      elsif a[2] == 'off'
         
     | 
| 
      
 12 
     | 
    
         
            +
                        Kogno::Messenger::Api.setting_delete([:persistent_menu])
         
     | 
| 
      
 13 
     | 
    
         
            +
                      end
         
     | 
| 
      
 14 
     | 
    
         
            +
                    when :get_started
         
     | 
| 
      
 15 
     | 
    
         
            +
                      if a[2] == 'on'
         
     | 
| 
      
 16 
     | 
    
         
            +
                        Kogno::Messenger::Api.get_started_button(page_id)
         
     | 
| 
      
 17 
     | 
    
         
            +
                      elsif a[2] == 'off'
         
     | 
| 
      
 18 
     | 
    
         
            +
                        Kogno::Messenger::Api.setting_delete([:get_started], page_id)
         
     | 
| 
      
 19 
     | 
    
         
            +
                      end
         
     | 
| 
      
 20 
     | 
    
         
            +
                    when :update_whitelisted_domains
         
     | 
| 
      
 21 
     | 
    
         
            +
                      Kogno::Messenger::Api.update_whitelisted_domains
         
     | 
| 
      
 22 
     | 
    
         
            +
                    when :ice_breakers
         
     | 
| 
      
 23 
     | 
    
         
            +
                      if a[2] == 'on'
         
     | 
| 
      
 24 
     | 
    
         
            +
                        Kogno::Messenger::Api.ice_breakers()
         
     | 
| 
      
 25 
     | 
    
         
            +
                      elsif a[2] == 'off'
         
     | 
| 
      
 26 
     | 
    
         
            +
                        Kogno::Messenger::Api.setting_delete([:ice_breakers])
         
     | 
| 
      
 27 
     | 
    
         
            +
                      end          
         
     | 
| 
      
 28 
     | 
    
         
            +
                    when :greeting
         
     | 
| 
      
 29 
     | 
    
         
            +
                      if a[2] == 'on'
         
     | 
| 
      
 30 
     | 
    
         
            +
                        Kogno::Messenger::Api.greeting()
         
     | 
| 
      
 31 
     | 
    
         
            +
                      elsif a[2] == 'off'
         
     | 
| 
      
 32 
     | 
    
         
            +
                        Kogno::Messenger::Api.setting_delete([:greting])
         
     | 
| 
      
 33 
     | 
    
         
            +
                      end         
         
     | 
| 
      
 34 
     | 
    
         
            +
                    else
         
     | 
| 
      
 35 
     | 
    
         
            +
                      puts %{Usage: 
         
     | 
| 
      
 36 
     | 
    
         
            +
                        kogno messenger 
         
     | 
| 
      
 37 
     | 
    
         
            +
                            menu on|off
         
     | 
| 
      
 38 
     | 
    
         
            +
                            get_started on|off
         
     | 
| 
      
 39 
     | 
    
         
            +
                            greeting on|off
         
     | 
| 
      
 40 
     | 
    
         
            +
                            ice_breakers on|off                
         
     | 
| 
      
 41 
     | 
    
         
            +
                            update_whitelisted_domains
         
     | 
| 
      
 42 
     | 
    
         
            +
                      }      
         
     | 
| 
      
 43 
     | 
    
         
            +
                  end
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
              end
         
     | 
| 
      
 47 
     | 
    
         
            +
            end
         
     | 
| 
      
 48 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,203 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'fileutils'
         
     | 
| 
      
 2 
     | 
    
         
            +
            module Kogno
         
     | 
| 
      
 3 
     | 
    
         
            +
            class Scaffolding
         
     | 
| 
      
 4 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                def new_project
         
     | 
| 
      
 7 
     | 
    
         
            +
                  project_name = ARGV[1]
         
     | 
| 
      
 8 
     | 
    
         
            +
                  FileUtils.mkdir(project_name)
         
     | 
| 
      
 9 
     | 
    
         
            +
                  FileUtils.cp_r("#{Application.core_path}/scaffolding/new_project/.","./#{project_name}/")
         
     | 
| 
      
 10 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/config/initializers")
         
     | 
| 
      
 11 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/bot/templates")
         
     | 
| 
      
 12 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/bot/templates/main")
         
     | 
| 
      
 13 
     | 
    
         
            +
                  # FileUtils.mkdir("./#{project_name}/bot/extensions")
         
     | 
| 
      
 14 
     | 
    
         
            +
                  # FileUtils.mkdir("./#{project_name}/bot/models")
         
     | 
| 
      
 15 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/bot/helpers")
         
     | 
| 
      
 16 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/lib")
         
     | 
| 
      
 17 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/logs")
         
     | 
| 
      
 18 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/tmp")
         
     | 
| 
      
 19 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/web/public")
         
     | 
| 
      
 20 
     | 
    
         
            +
                  FileUtils.mkdir("./#{project_name}/web/views")
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                  logger.write "\nA new project has been created at ./#{project_name}", :green
         
     | 
| 
      
 23 
     | 
    
         
            +
                  logger.write "Next steps:", :bright
         
     | 
| 
      
 24 
     | 
    
         
            +
                  logger.write "  - cd ./#{project_name}/", :pink
         
     | 
| 
      
 25 
     | 
    
         
            +
                  logger.write "  - bundle install", :pink
         
     | 
| 
      
 26 
     | 
    
         
            +
                  logger.write "  - Configure the database in config/database.yml", :pink
         
     | 
| 
      
 27 
     | 
    
         
            +
                  logger.write "  - kogno install", :pink
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                def install
         
     | 
| 
      
 31 
     | 
    
         
            +
                  if !self.test_database
         
     | 
| 
      
 32 
     | 
    
         
            +
                    logger.write "ERROR: check your database configuration in config/database.yml", :red
         
     | 
| 
      
 33 
     | 
    
         
            +
                  else
         
     | 
| 
      
 34 
     | 
    
         
            +
                    self.create_tables
         
     | 
| 
      
 35 
     | 
    
         
            +
                    logger.write "\nNow, you can configure:\n   config/application.rb\n\nAlso some or all these platforms:\n  config/platforms/messenger.rb \n  config/platforms/telegram.rb \n  config/platforms/whatsapp.rb \n  config/nlp.rb\n", :bright        
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def test_database
         
     | 
| 
      
 40 
     | 
    
         
            +
                  require 'core/db'
         
     | 
| 
      
 41 
     | 
    
         
            +
                  ActiveRecord::Base.connection rescue nil
         
     | 
| 
      
 42 
     | 
    
         
            +
                  return ActiveRecord::Base.connected?
         
     | 
| 
      
 43 
     | 
    
         
            +
                end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                def create_tables
         
     | 
| 
      
 46 
     | 
    
         
            +
                  logger.write "Creating tables..", :bright
         
     | 
| 
      
 47 
     | 
    
         
            +
                  logger.write "   users", :green
         
     | 
| 
      
 48 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 49 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS users (
         
     | 
| 
      
 50 
     | 
    
         
            +
                      id int(10) unsigned NOT NULL AUTO_INCREMENT,
         
     | 
| 
      
 51 
     | 
    
         
            +
                      psid varchar(50),
         
     | 
| 
      
 52 
     | 
    
         
            +
                      platform varchar(50),
         
     | 
| 
      
 53 
     | 
    
         
            +
                      psid_from_post_comment varchar(50),
         
     | 
| 
      
 54 
     | 
    
         
            +
                      page_id varchar(50),          
         
     | 
| 
      
 55 
     | 
    
         
            +
                      name varchar(55), 
         
     | 
| 
      
 56 
     | 
    
         
            +
                      first_name varchar(55), 
         
     | 
| 
      
 57 
     | 
    
         
            +
                      last_name varchar(55), 
         
     | 
| 
      
 58 
     | 
    
         
            +
                      timezone varchar(10), 
         
     | 
| 
      
 59 
     | 
    
         
            +
                      locale varchar(5),
         
     | 
| 
      
 60 
     | 
    
         
            +
                      last_usage_at datetime,
         
     | 
| 
      
 61 
     | 
    
         
            +
                      created_at datetime,
         
     | 
| 
      
 62 
     | 
    
         
            +
                      updated_at datetime,
         
     | 
| 
      
 63 
     | 
    
         
            +
                      context varchar(120) DEFAULT NULL,
         
     | 
| 
      
 64 
     | 
    
         
            +
                      context_params text,
         
     | 
| 
      
 65 
     | 
    
         
            +
                      session_vars text,
         
     | 
| 
      
 66 
     | 
    
         
            +
                      last_message_read boolean default false,
         
     | 
| 
      
 67 
     | 
    
         
            +
                      PRIMARY KEY (id),
         
     | 
| 
      
 68 
     | 
    
         
            +
                      UNIQUE KEY psid (psid),
         
     | 
| 
      
 69 
     | 
    
         
            +
                      UNIQUE KEY token (psid_from_post_comment),
         
     | 
| 
      
 70 
     | 
    
         
            +
                      KEY(page_id),
         
     | 
| 
      
 71 
     | 
    
         
            +
                      KEY(platform)
         
     | 
| 
      
 72 
     | 
    
         
            +
                    ) ENGINE=MyISAM
         
     | 
| 
      
 73 
     | 
    
         
            +
                  })
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                  logger.write "   kogno_sequences", :green
         
     | 
| 
      
 76 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 77 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_sequences(
         
     | 
| 
      
 78 
     | 
    
         
            +
                    	id int(10) unsigned NOT NULL AUTO_INCREMENT,
         
     | 
| 
      
 79 
     | 
    
         
            +
                    	user_id int unsigned,
         
     | 
| 
      
 80 
     | 
    
         
            +
                    	stage varchar(60),
         
     | 
| 
      
 81 
     | 
    
         
            +
                    	context varchar(60),
         
     | 
| 
      
 82 
     | 
    
         
            +
                    	last_executed int default 0,
         
     | 
| 
      
 83 
     | 
    
         
            +
                    	last_hit_at datetime,
         
     | 
| 
      
 84 
     | 
    
         
            +
                    	execution_time datetime default NULL,
         
     | 
| 
      
 85 
     | 
    
         
            +
                    	created_at datetime,
         
     | 
| 
      
 86 
     | 
    
         
            +
                    	updated_at datetime,
         
     | 
| 
      
 87 
     | 
    
         
            +
                    	primary key(id),
         
     | 
| 
      
 88 
     | 
    
         
            +
                    	unique key user_context(user_id, context)
         
     | 
| 
      
 89 
     | 
    
         
            +
                    ) ENGINE=MyISAM
         
     | 
| 
      
 90 
     | 
    
         
            +
                  })
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                  logger.write "   kogno_chat_logs", :green
         
     | 
| 
      
 93 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 94 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_chat_logs (
         
     | 
| 
      
 95 
     | 
    
         
            +
                      id int(10) unsigned NOT NULL AUTO_INCREMENT,
         
     | 
| 
      
 96 
     | 
    
         
            +
                      user_id int(10) unsigned DEFAULT NULL,
         
     | 
| 
      
 97 
     | 
    
         
            +
                      message_type varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL,
         
     | 
| 
      
 98 
     | 
    
         
            +
                      body text COLLATE utf8mb4_general_ci,
         
     | 
| 
      
 99 
     | 
    
         
            +
                      context varchar(120) COLLATE utf8mb4_general_ci DEFAULT NULL,
         
     | 
| 
      
 100 
     | 
    
         
            +
                      message varchar(1024) COLLATE utf8mb4_general_ci DEFAULT NULL,
         
     | 
| 
      
 101 
     | 
    
         
            +
                      payload varchar(120) COLLATE utf8mb4_general_ci DEFAULT NULL,
         
     | 
| 
      
 102 
     | 
    
         
            +
                      payload_params varchar(1024) COLLATE utf8mb4_general_ci DEFAULT NULL,
         
     | 
| 
      
 103 
     | 
    
         
            +
                      nlp_entities text COLLATE utf8mb4_general_ci,
         
     | 
| 
      
 104 
     | 
    
         
            +
                      user_vars text COLLATE utf8mb4_general_ci,
         
     | 
| 
      
 105 
     | 
    
         
            +
                      response text,
         
     | 
| 
      
 106 
     | 
    
         
            +
                      new_user boolean default false,
         
     | 
| 
      
 107 
     | 
    
         
            +
                      processed int(1) unsigned DEFAULT 0,
         
     | 
| 
      
 108 
     | 
    
         
            +
                      scheduled boolean default false,
         
     | 
| 
      
 109 
     | 
    
         
            +
                      created_at datetime DEFAULT NULL,
         
     | 
| 
      
 110 
     | 
    
         
            +
                      updated_at datetime DEFAULT NULL,
         
     | 
| 
      
 111 
     | 
    
         
            +
                      PRIMARY KEY (id),
         
     | 
| 
      
 112 
     | 
    
         
            +
                      KEY user_id (user_id)
         
     | 
| 
      
 113 
     | 
    
         
            +
                    ) ENGINE=MyISAM
         
     | 
| 
      
 114 
     | 
    
         
            +
                  })
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                  logger.write "   kogno_scheduled_messages", :green
         
     | 
| 
      
 117 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 118 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_scheduled_messages(
         
     | 
| 
      
 119 
     | 
    
         
            +
                      id int(10) unsigned NOT NULL AUTO_INCREMENT,
         
     | 
| 
      
 120 
     | 
    
         
            +
                      user_id int(10) unsigned DEFAULT NULL,
         
     | 
| 
      
 121 
     | 
    
         
            +
                      tag varchar(24) default null,
         
     | 
| 
      
 122 
     | 
    
         
            +
                      messages text,
         
     | 
| 
      
 123 
     | 
    
         
            +
                      send_at datetime,
         
     | 
| 
      
 124 
     | 
    
         
            +
                      created_at datetime,
         
     | 
| 
      
 125 
     | 
    
         
            +
                      updated_at datetime,
         
     | 
| 
      
 126 
     | 
    
         
            +
                      PRIMARY KEY (id),
         
     | 
| 
      
 127 
     | 
    
         
            +
                      KEY user_id (user_id),
         
     | 
| 
      
 128 
     | 
    
         
            +
                      KEY tag (tag)
         
     | 
| 
      
 129 
     | 
    
         
            +
                    ) ENGINE=MyISAM
         
     | 
| 
      
 130 
     | 
    
         
            +
                  })
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                  logger.write "   kogno_matched_messages", :green
         
     | 
| 
      
 133 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 134 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_matched_messages (
         
     | 
| 
      
 135 
     | 
    
         
            +
                      id int(11) unsigned primary key auto_increment,
         
     | 
| 
      
 136 
     | 
    
         
            +
                      user_id int(11) unsigned not null,
         
     | 
| 
      
 137 
     | 
    
         
            +
                      platform_message_id int(11) unsigned default 0,
         
     | 
| 
      
 138 
     | 
    
         
            +
                      created_at datetime,
         
     | 
| 
      
 139 
     | 
    
         
            +
                      updated_at datetime,
         
     | 
| 
      
 140 
     | 
    
         
            +
                      key(user_id),
         
     | 
| 
      
 141 
     | 
    
         
            +
                      key(platform_message_id)
         
     | 
| 
      
 142 
     | 
    
         
            +
                    ) ENGINE=MyISAM 
         
     | 
| 
      
 143 
     | 
    
         
            +
                  })      
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
                  logger.write "   kogno_telegram_chat_groups", :green
         
     | 
| 
      
 146 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 147 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_telegram_chat_groups (
         
     | 
| 
      
 148 
     | 
    
         
            +
                      id int(11) unsigned primary key auto_increment,
         
     | 
| 
      
 149 
     | 
    
         
            +
                      chat_id bigint not null,
         
     | 
| 
      
 150 
     | 
    
         
            +
                      title varchar(256),
         
     | 
| 
      
 151 
     | 
    
         
            +
                      kind enum('group','supergroup','channel'),
         
     | 
| 
      
 152 
     | 
    
         
            +
                      membership boolean,
         
     | 
| 
      
 153 
     | 
    
         
            +
                      perms varchar(1024),
         
     | 
| 
      
 154 
     | 
    
         
            +
                      inviter_user_id int unsigned not null,
         
     | 
| 
      
 155 
     | 
    
         
            +
                      created_at datetime,
         
     | 
| 
      
 156 
     | 
    
         
            +
                      updated_at datetime,
         
     | 
| 
      
 157 
     | 
    
         
            +
                      unique key(chat_id)
         
     | 
| 
      
 158 
     | 
    
         
            +
                    ) ENGINE=MyISAM 
         
     | 
| 
      
 159 
     | 
    
         
            +
                  })
         
     | 
| 
      
 160 
     | 
    
         
            +
             
     | 
| 
      
 161 
     | 
    
         
            +
                  logger.write "   kogno_long_payloads", :green
         
     | 
| 
      
 162 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 163 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_long_payloads(
         
     | 
| 
      
 164 
     | 
    
         
            +
                      id int(11) unsigned primary key auto_increment,
         
     | 
| 
      
 165 
     | 
    
         
            +
                      token varchar(32),
         
     | 
| 
      
 166 
     | 
    
         
            +
                      payload varchar(64),
         
     | 
| 
      
 167 
     | 
    
         
            +
                      params text,
         
     | 
| 
      
 168 
     | 
    
         
            +
                      unique key token(token),
         
     | 
| 
      
 169 
     | 
    
         
            +
                      created_at datetime,
         
     | 
| 
      
 170 
     | 
    
         
            +
                      updated_at datetime
         
     | 
| 
      
 171 
     | 
    
         
            +
                    ) ENGINE=MyISAM
         
     | 
| 
      
 172 
     | 
    
         
            +
                  })
         
     | 
| 
      
 173 
     | 
    
         
            +
             
     | 
| 
      
 174 
     | 
    
         
            +
                  logger.write "   kogno_messenger_recurring_notifications", :green
         
     | 
| 
      
 175 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute(%{
         
     | 
| 
      
 176 
     | 
    
         
            +
                    CREATE TABLE IF NOT EXISTS kogno_messenger_recurring_notifications(
         
     | 
| 
      
 177 
     | 
    
         
            +
                      id int unsigned auto_increment primary key,
         
     | 
| 
      
 178 
     | 
    
         
            +
                      user_id int unsigned,
         
     | 
| 
      
 179 
     | 
    
         
            +
                      token varchar(64),
         
     | 
| 
      
 180 
     | 
    
         
            +
                      frecuency varchar(32),
         
     | 
| 
      
 181 
     | 
    
         
            +
                      expires_at datetime,
         
     | 
| 
      
 182 
     | 
    
         
            +
                      token_status varchar(32),
         
     | 
| 
      
 183 
     | 
    
         
            +
                      timezone varchar(16),
         
     | 
| 
      
 184 
     | 
    
         
            +
                      active boolean,
         
     | 
| 
      
 185 
     | 
    
         
            +
                      key(user_id),
         
     | 
| 
      
 186 
     | 
    
         
            +
                      key(expires_at),
         
     | 
| 
      
 187 
     | 
    
         
            +
                      created_at datetime,
         
     | 
| 
      
 188 
     | 
    
         
            +
                      updated_at datetime
         
     | 
| 
      
 189 
     | 
    
         
            +
                    ) ENGINE=MyISAM 
         
     | 
| 
      
 190 
     | 
    
         
            +
                  })
         
     | 
| 
      
 191 
     | 
    
         
            +
               
         
     | 
| 
      
 192 
     | 
    
         
            +
             
     | 
| 
      
 193 
     | 
    
         
            +
                  # logger.write "   Changin database encoding..", :green
         
     | 
| 
      
 194 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute("ALTER DATABASE #{ActiveRecord::Base.connection.current_database} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci")
         
     | 
| 
      
 195 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute("ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
         
     | 
| 
      
 196 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute("ALTER TABLE users MODIFY session_vars text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
         
     | 
| 
      
 197 
     | 
    
         
            +
                  ActiveRecord::Base.connection.execute("ALTER TABLE kogno_scheduled_messages MODIFY messages text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
         
     | 
| 
      
 198 
     | 
    
         
            +
              
         
     | 
| 
      
 199 
     | 
    
         
            +
                end
         
     | 
| 
      
 200 
     | 
    
         
            +
             
     | 
| 
      
 201 
     | 
    
         
            +
              end
         
     | 
| 
      
 202 
     | 
    
         
            +
            end
         
     | 
| 
      
 203 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,95 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Kogno
         
     | 
| 
      
 2 
     | 
    
         
            +
            class ScheduledMessagesCtl
         
     | 
| 
      
 3 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def log_environment_info
         
     | 
| 
      
 6 
     | 
    
         
            +
                  logger.write "Kogno #{Gem.loaded_specs["kogno"].version.version} scheduled_messages server starting in #{Kogno::Application.config.environment}", :bright
         
     | 
| 
      
 7 
     | 
    
         
            +
                end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                def pid_file
         
     | 
| 
      
 10 
     | 
    
         
            +
                  Application.file_path(File.join(Application.project_path,"tmp","scheduled_messages.pid"))
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def save_pid(pid)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  File.write(pid_file, pid)
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def delete_pid
         
     | 
| 
      
 18 
     | 
    
         
            +
                  save_pid("")
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                def get_pid
         
     | 
| 
      
 22 
     | 
    
         
            +
                  File.read(File.join(pid_file)).to_i
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                def kill_process(pid)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  return false if pid.to_i == 0
         
     | 
| 
      
 27 
     | 
    
         
            +
                  Process.kill("KILL",pid) rescue nil
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                def alive?
         
     | 
| 
      
 31 
     | 
    
         
            +
                  pid = get_pid
         
     | 
| 
      
 32 
     | 
    
         
            +
                  return false if pid == 0
         
     | 
| 
      
 33 
     | 
    
         
            +
                  Process.getpgid(pid) rescue false
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                def start
         
     | 
| 
      
 37 
     | 
    
         
            +
                  log_environment_info()
         
     | 
| 
      
 38 
     | 
    
         
            +
                  if get_pid > 0 && alive?
         
     | 
| 
      
 39 
     | 
    
         
            +
                    logger.write "Error: It has already started..", :red
         
     | 
| 
      
 40 
     | 
    
         
            +
                  else
         
     | 
| 
      
 41 
     | 
    
         
            +
                    logger.write "START", :green
         
     | 
| 
      
 42 
     | 
    
         
            +
                    pid = fork do           
         
     | 
| 
      
 43 
     | 
    
         
            +
                      Logger.set(:scheduled_messages)
         
     | 
| 
      
 44 
     | 
    
         
            +
                      require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 45 
     | 
    
         
            +
                      ScheduledMessage.process_all
         
     | 
| 
      
 46 
     | 
    
         
            +
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
                    save_pid(pid)
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                def fg
         
     | 
| 
      
 52 
     | 
    
         
            +
                  log_environment_info()
         
     | 
| 
      
 53 
     | 
    
         
            +
                  if get_pid > 0 && alive?
         
     | 
| 
      
 54 
     | 
    
         
            +
                    logger.write "Error: It has already started..", :red
         
     | 
| 
      
 55 
     | 
    
         
            +
                  else
         
     | 
| 
      
 56 
     | 
    
         
            +
                    require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 57 
     | 
    
         
            +
                    ScheduledMessage.process_all
         
     | 
| 
      
 58 
     | 
    
         
            +
                  end
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                def stop
         
     | 
| 
      
 62 
     | 
    
         
            +
                  pid = get_pid
         
     | 
| 
      
 63 
     | 
    
         
            +
                  kill_process(pid)
         
     | 
| 
      
 64 
     | 
    
         
            +
                  delete_pid
         
     | 
| 
      
 65 
     | 
    
         
            +
                  logger.write "STOP", :red
         
     | 
| 
      
 66 
     | 
    
         
            +
                end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
                def status
         
     | 
| 
      
 69 
     | 
    
         
            +
                  if alive?
         
     | 
| 
      
 70 
     | 
    
         
            +
                    logger.write "Running | Pid: #{get_pid}..", :green
         
     | 
| 
      
 71 
     | 
    
         
            +
                  else
         
     | 
| 
      
 72 
     | 
    
         
            +
                    logger.write "Stopped", :red
         
     | 
| 
      
 73 
     | 
    
         
            +
                  end
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                def options(option)
         
     | 
| 
      
 77 
     | 
    
         
            +
                  case option.to_s.to_sym
         
     | 
| 
      
 78 
     | 
    
         
            +
                    when :start
         
     | 
| 
      
 79 
     | 
    
         
            +
                      start
         
     | 
| 
      
 80 
     | 
    
         
            +
                    when :stop
         
     | 
| 
      
 81 
     | 
    
         
            +
                      stop
         
     | 
| 
      
 82 
     | 
    
         
            +
                    when :fg
         
     | 
| 
      
 83 
     | 
    
         
            +
                      fg
         
     | 
| 
      
 84 
     | 
    
         
            +
                    when :status
         
     | 
| 
      
 85 
     | 
    
         
            +
                      status
         
     | 
| 
      
 86 
     | 
    
         
            +
                    when :restart
         
     | 
| 
      
 87 
     | 
    
         
            +
                      stop
         
     | 
| 
      
 88 
     | 
    
         
            +
                      start
         
     | 
| 
      
 89 
     | 
    
         
            +
                    else
         
     | 
| 
      
 90 
     | 
    
         
            +
                      puts "usage: scheduled_messages stop|start|restart|status|fg"
         
     | 
| 
      
 91 
     | 
    
         
            +
                  end
         
     | 
| 
      
 92 
     | 
    
         
            +
                end
         
     | 
| 
      
 93 
     | 
    
         
            +
              end
         
     | 
| 
      
 94 
     | 
    
         
            +
            end
         
     | 
| 
      
 95 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,95 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Kogno
         
     | 
| 
      
 2 
     | 
    
         
            +
            class SequencesCtl
         
     | 
| 
      
 3 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def pid_file
         
     | 
| 
      
 6 
     | 
    
         
            +
                  Application.file_path(File.join(Application.project_path,"tmp","sequence.pid"))
         
     | 
| 
      
 7 
     | 
    
         
            +
                end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                def log_environment_info
         
     | 
| 
      
 10 
     | 
    
         
            +
                  logger.write "Kogno #{Gem.loaded_specs["kogno"].version.version} sequences server starting in #{Kogno::Application.config.environment}", :bright
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def save_pid(pid)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  File.write(pid_file, pid)
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def delete_pid
         
     | 
| 
      
 18 
     | 
    
         
            +
                  save_pid("")
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                def get_pid
         
     | 
| 
      
 22 
     | 
    
         
            +
                  File.read(File.join(pid_file)).to_i
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                def kill_process(pid)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  return false if pid.to_i == 0
         
     | 
| 
      
 27 
     | 
    
         
            +
                  Process.kill("KILL",pid) rescue nil
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                def alive?
         
     | 
| 
      
 31 
     | 
    
         
            +
                  pid = get_pid
         
     | 
| 
      
 32 
     | 
    
         
            +
                  return false if pid == 0
         
     | 
| 
      
 33 
     | 
    
         
            +
                  Process.getpgid(pid) rescue false
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                def start
         
     | 
| 
      
 37 
     | 
    
         
            +
                  if get_pid > 0 && alive?
         
     | 
| 
      
 38 
     | 
    
         
            +
                    logger.write "Error: It has already started..", :red
         
     | 
| 
      
 39 
     | 
    
         
            +
                  else 
         
     | 
| 
      
 40 
     | 
    
         
            +
                    log_environment_info()     
         
     | 
| 
      
 41 
     | 
    
         
            +
                    logger.write "START", :green
         
     | 
| 
      
 42 
     | 
    
         
            +
                    pid = fork do
         
     | 
| 
      
 43 
     | 
    
         
            +
                      Logger.set(:sequences)
         
     | 
| 
      
 44 
     | 
    
         
            +
                      require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 45 
     | 
    
         
            +
                      Sequence.process_all
         
     | 
| 
      
 46 
     | 
    
         
            +
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
                    save_pid(pid)
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                def fg
         
     | 
| 
      
 52 
     | 
    
         
            +
                  log_environment_info()
         
     | 
| 
      
 53 
     | 
    
         
            +
                  if get_pid > 0 && alive?
         
     | 
| 
      
 54 
     | 
    
         
            +
                    logger.write "Error: It has already started..", :red
         
     | 
| 
      
 55 
     | 
    
         
            +
                  else        
         
     | 
| 
      
 56 
     | 
    
         
            +
                    require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 57 
     | 
    
         
            +
                    Sequence.process_all
         
     | 
| 
      
 58 
     | 
    
         
            +
                  end
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                def stop
         
     | 
| 
      
 62 
     | 
    
         
            +
                  pid = get_pid
         
     | 
| 
      
 63 
     | 
    
         
            +
                  kill_process(pid)
         
     | 
| 
      
 64 
     | 
    
         
            +
                  delete_pid
         
     | 
| 
      
 65 
     | 
    
         
            +
                  logger.write "STOP", :red
         
     | 
| 
      
 66 
     | 
    
         
            +
                end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
                def status
         
     | 
| 
      
 69 
     | 
    
         
            +
                  if alive?
         
     | 
| 
      
 70 
     | 
    
         
            +
                    logger.write "Running | Pid: #{get_pid}..", :green
         
     | 
| 
      
 71 
     | 
    
         
            +
                  else
         
     | 
| 
      
 72 
     | 
    
         
            +
                    logger.write "Stopped", :red
         
     | 
| 
      
 73 
     | 
    
         
            +
                  end
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                def options(option)
         
     | 
| 
      
 77 
     | 
    
         
            +
                  case option.to_s.to_sym
         
     | 
| 
      
 78 
     | 
    
         
            +
                    when :start
         
     | 
| 
      
 79 
     | 
    
         
            +
                      start
         
     | 
| 
      
 80 
     | 
    
         
            +
                    when :stop
         
     | 
| 
      
 81 
     | 
    
         
            +
                      stop
         
     | 
| 
      
 82 
     | 
    
         
            +
                    when :fg
         
     | 
| 
      
 83 
     | 
    
         
            +
                      fg
         
     | 
| 
      
 84 
     | 
    
         
            +
                    when :status
         
     | 
| 
      
 85 
     | 
    
         
            +
                      status
         
     | 
| 
      
 86 
     | 
    
         
            +
                    when :restart
         
     | 
| 
      
 87 
     | 
    
         
            +
                      stop
         
     | 
| 
      
 88 
     | 
    
         
            +
                      start
         
     | 
| 
      
 89 
     | 
    
         
            +
                    else
         
     | 
| 
      
 90 
     | 
    
         
            +
                      puts "usage: sequences stop|start|restart|status|fg"
         
     | 
| 
      
 91 
     | 
    
         
            +
                  end
         
     | 
| 
      
 92 
     | 
    
         
            +
                end
         
     | 
| 
      
 93 
     | 
    
         
            +
              end
         
     | 
| 
      
 94 
     | 
    
         
            +
            end
         
     | 
| 
      
 95 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,127 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Kogno
         
     | 
| 
      
 2 
     | 
    
         
            +
            class ServerCtl
         
     | 
| 
      
 3 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def log_environment_info
         
     | 
| 
      
 6 
     | 
    
         
            +
                  logger.write "Kogno #{Gem.loaded_specs["kogno"].version.version} server starting in #{Kogno::Application.config.environment}", :bright
         
     | 
| 
      
 7 
     | 
    
         
            +
                end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                def pid_file(service)
         
     | 
| 
      
 10 
     | 
    
         
            +
                  case service
         
     | 
| 
      
 11 
     | 
    
         
            +
                    when 'webhook'
         
     | 
| 
      
 12 
     | 
    
         
            +
                      return Application.file_path(File.join(Application.project_path,"tmp","webhook.pid"))
         
     | 
| 
      
 13 
     | 
    
         
            +
                    when 'sequence'
         
     | 
| 
      
 14 
     | 
    
         
            +
                      return Application.file_path(File.join(Application.project_path,"tmp","sequence.pid"))
         
     | 
| 
      
 15 
     | 
    
         
            +
                    when 'scheduled_messages'  
         
     | 
| 
      
 16 
     | 
    
         
            +
                      return Application.file_path(File.join(Application.project_path,"tmp","scheduled_messages.pid"))
         
     | 
| 
      
 17 
     | 
    
         
            +
                    else
         
     | 
| 
      
 18 
     | 
    
         
            +
                      return 0
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
                  
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                def webhook_server_file
         
     | 
| 
      
 24 
     | 
    
         
            +
                  File.join(Application.core_path,"lib/core/web/webhook.rb")
         
     | 
| 
      
 25 
     | 
    
         
            +
                end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                def save_pid(pid, service)
         
     | 
| 
      
 28 
     | 
    
         
            +
                  File.write(pid_file(service), pid)      
         
     | 
| 
      
 29 
     | 
    
         
            +
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                def delete_pid(service)
         
     | 
| 
      
 32 
     | 
    
         
            +
                  save_pid("",service)
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                def get_pid(service)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  File.read(File.join(pid_file(service))).to_i
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def kill_process(pid)
         
     | 
| 
      
 40 
     | 
    
         
            +
                  return false if pid.to_i == 0
         
     | 
| 
      
 41 
     | 
    
         
            +
                  Process.kill("KILL",pid) rescue nil
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                def alive?(service)
         
     | 
| 
      
 45 
     | 
    
         
            +
                   pid = get_pid(service)
         
     | 
| 
      
 46 
     | 
    
         
            +
                   return false if pid == 0
         
     | 
| 
      
 47 
     | 
    
         
            +
                   Process.getpgid(pid) rescue false
         
     | 
| 
      
 48 
     | 
    
         
            +
                end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                def start
         
     | 
| 
      
 51 
     | 
    
         
            +
                  log_environment_info()
         
     | 
| 
      
 52 
     | 
    
         
            +
                  if get_pid('webhook') > 0
         
     | 
| 
      
 53 
     | 
    
         
            +
                    logger.write "Http: daemon already started..", :red
         
     | 
| 
      
 54 
     | 
    
         
            +
                  else
         
     | 
| 
      
 55 
     | 
    
         
            +
                    logger.write "Http: starting daemon..", :green
         
     | 
| 
      
 56 
     | 
    
         
            +
                    system(%{
         
     | 
| 
      
 57 
     | 
    
         
            +
                      RACK_ENV=#{Kogno::Application.config.environment} ruby #{self.webhook_server_file} daemon > /dev/null&
         
     | 
| 
      
 58 
     | 
    
         
            +
                      echo $! > "#{self.pid_file('webhook')}"
         
     | 
| 
      
 59 
     | 
    
         
            +
                    })
         
     | 
| 
      
 60 
     | 
    
         
            +
                  end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                  if get_pid('sequence') > 0
         
     | 
| 
      
 63 
     | 
    
         
            +
                    logger.write "Sequence: daemon already started..", :red
         
     | 
| 
      
 64 
     | 
    
         
            +
                  else
         
     | 
| 
      
 65 
     | 
    
         
            +
                    logger.write "Sequence: starting daemon..", :green
         
     | 
| 
      
 66 
     | 
    
         
            +
                    pid = fork do
         
     | 
| 
      
 67 
     | 
    
         
            +
                      Logger.set(:sequences)
         
     | 
| 
      
 68 
     | 
    
         
            +
                      require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 69 
     | 
    
         
            +
                      Sequence.process_all
         
     | 
| 
      
 70 
     | 
    
         
            +
                    end
         
     | 
| 
      
 71 
     | 
    
         
            +
                    save_pid(pid, 'sequence')
         
     | 
| 
      
 72 
     | 
    
         
            +
                  end
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
                  if get_pid('scheduled_messages') > 0
         
     | 
| 
      
 75 
     | 
    
         
            +
                    logger.write "Scheduled Messages: daemon already started..", :red
         
     | 
| 
      
 76 
     | 
    
         
            +
                  else
         
     | 
| 
      
 77 
     | 
    
         
            +
                    logger.write "Scheduled Messages: starting daemon..", :green
         
     | 
| 
      
 78 
     | 
    
         
            +
                    pid = fork do
         
     | 
| 
      
 79 
     | 
    
         
            +
                      Logger.set(:scheduled_messages)
         
     | 
| 
      
 80 
     | 
    
         
            +
                      require File.join(Kogno::Application.project_path,'application.rb')
         
     | 
| 
      
 81 
     | 
    
         
            +
                      ScheduledMessage.process_all
         
     | 
| 
      
 82 
     | 
    
         
            +
                    end
         
     | 
| 
      
 83 
     | 
    
         
            +
                    save_pid(pid, 'scheduled_messages')
         
     | 
| 
      
 84 
     | 
    
         
            +
                  end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                end
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
                def stop
         
     | 
| 
      
 89 
     | 
    
         
            +
                  sequence_pid = get_pid('sequence')
         
     | 
| 
      
 90 
     | 
    
         
            +
                  kill_process(sequence_pid)
         
     | 
| 
      
 91 
     | 
    
         
            +
                  delete_pid('sequence')
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
                  webhook_pid = get_pid('webhook')
         
     | 
| 
      
 94 
     | 
    
         
            +
                  kill_process(webhook_pid)
         
     | 
| 
      
 95 
     | 
    
         
            +
                  delete_pid('webhook')
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                  scheduled_messages_pid = get_pid('scheduled_messages')
         
     | 
| 
      
 98 
     | 
    
         
            +
                  kill_process(scheduled_messages_pid)
         
     | 
| 
      
 99 
     | 
    
         
            +
                  delete_pid('scheduled_messages')
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                  logger.write "STOP", :red
         
     | 
| 
      
 102 
     | 
    
         
            +
                end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
                def status
         
     | 
| 
      
 105 
     | 
    
         
            +
                  alive?('webhook') ? logger.write("Http running | Pid: #{get_pid('webhook')}..", :green) : logger.write("Http stopped", :red)
         
     | 
| 
      
 106 
     | 
    
         
            +
                  alive?('sequence') ? logger.write("Sequences running | Pid: #{get_pid('sequence')}..", :green) : logger.write("Sequence stopped", :red)
         
     | 
| 
      
 107 
     | 
    
         
            +
                  alive?('scheduled_messages') ? logger.write("Scheduled Messages running | Pid: #{get_pid('scheduled_messages')}..", :green) : logger.write("Scheduled Messages stopped", :red)      
         
     | 
| 
      
 108 
     | 
    
         
            +
                end
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                def options(option)
         
     | 
| 
      
 111 
     | 
    
         
            +
                  case option.to_sym
         
     | 
| 
      
 112 
     | 
    
         
            +
                    when :start
         
     | 
| 
      
 113 
     | 
    
         
            +
                      start
         
     | 
| 
      
 114 
     | 
    
         
            +
                    when :stop
         
     | 
| 
      
 115 
     | 
    
         
            +
                      stop
         
     | 
| 
      
 116 
     | 
    
         
            +
                    when :status
         
     | 
| 
      
 117 
     | 
    
         
            +
                      status
         
     | 
| 
      
 118 
     | 
    
         
            +
                    when :restart
         
     | 
| 
      
 119 
     | 
    
         
            +
                      stop
         
     | 
| 
      
 120 
     | 
    
         
            +
                      start
         
     | 
| 
      
 121 
     | 
    
         
            +
                    else
         
     | 
| 
      
 122 
     | 
    
         
            +
                      puts "usage: server stop|start|restart|status"
         
     | 
| 
      
 123 
     | 
    
         
            +
                  end
         
     | 
| 
      
 124 
     | 
    
         
            +
                end
         
     | 
| 
      
 125 
     | 
    
         
            +
              end
         
     | 
| 
      
 126 
     | 
    
         
            +
            end
         
     | 
| 
      
 127 
     | 
    
         
            +
            end
         
     |