Class: Kanal::Interfaces::Pachka::PachkaInterface

Inherits:
Core::Interfaces::Interface
  • Object
show all
Includes:
Logger
Defined in:
lib/kanal/interfaces/pachka/pachka_interface.rb

Overview

Interface for Pachka messenger, to work with bots provided in the integration section of Pachka messenger. It starts web server and accepts requests on api endpoint and sends requests to the Pachka api for bot to actually respond. Input parameters registered:

input.pachka_command - command sent from users to bot. Full text of command passed, like "/hello"

Output parameters registered:

output.pachka_text - you can specify it in the respond block for bot to reply with text
output.pachka_file_path - you can specify it in the respond block for bot to reply with file

Instance Method Summary collapse

Constructor Details

#initialize(core, access_token, host: "localhost", port: 8090, local_server_log: false, api_debug_log: false) ⇒ PachkaInterface

Creates interface with core and optional parameters Be aware, it starts web server to accept Pachkas bot outgoing webhook requests

Parameters:

  • core (Kanal::Core::Core)

    Kanal core

  • api_token (String)

    access_token you obtained in Pachka integrations for bot

  • host (String) (defaults to: "localhost")

    host of web server accepting outgoing webhook requests from pachka bot

  • port (Integer) (defaults to: 8090)

    port of web server accepting outgoing webhook requests from pachka bot

  • local_server_debug_log (Boolean)

    pass true for local server to log requests to it to Kanal logger

  • api_debug_log (Boolean) (defaults to: false)

    pass true to log pachka api requests to STDOUT



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kanal/interfaces/pachka/pachka_interface.rb', line 39

def initialize(core, access_token, host: "localhost", port: 8090, local_server_log: false, api_debug_log: false)
  super(core)

  @port = port
  @host = host

  @local_server_log = local_server_log

  @api = Kanal::Interfaces::Pachka::Helpers::PachkaApi.new access_token, verbose: api_debug_log

  @access_token = access_token

  core.register_plugin Kanal::Plugins::Batteries::BatteriesPlugin.new
  core.register_plugin Kanal::Interfaces::Pachka::Plugins::PachkaIntegrationPlugin.new
end

Instance Method Details

#consume_output(output) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kanal/interfaces/pachka/pachka_interface.rb', line 55

def consume_output(output)
  text = output.pachka_text

  uploaded_files = []

  unless output.pachka_file_path.nil?
    uploaded_file = @api.upload_file output.pachka_file_path

    uploaded_files << uploaded_file
  end

  # When sending files without text
  text ||= " "

  @api.send_message(
    output.pachka_entity_id,
    output.pachka_entity_type,
    text,
    uploaded_files
  )
rescue Exception => e
  logger.error "Error sending output as message to Pachka api! More info: #{e.full_message}"

  begin
    @api.send_message(
      output.pachka_entity_id,
      output.pachka_entity_type,
      "Error occured while sending message to Pachka api. Please consult with developers of this bot"
    )
  rescue Exception => e
    logger.fatal "Can't even send message about erro to Pachka api! More info: #{e.full_message}"
  end
end

#startObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kanal/interfaces/pachka/pachka_interface.rb', line 89

def start
  logger.info "Starting Pachka interface on http://#{@host}:#{@port}"

  endpoint = Kanal::Interfaces::Pachka::Helpers::LocalServer.new(@host, @port)
  endpoint.on_request do |body|
    logger.debug "Local server received request with body: #{body}" if @local_server_log

    input = core.create_input
    input.source = :pachka
    input.pachka_entity_type = body["entity_type"]
    input.pachka_entity_id = body["entity_id"]
    input.pachka_query = body["content"]

    consume_input input
  end
  endpoint.start_accepting_requests
end