Class: Fastlane::Actions::Teletopic

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/teletopic/actions/teletopic_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



68
69
70
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 68

def self.authors
  ["sergpetrov"]
end

.available_optionsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 80

def self.available_options
  [
             FastlaneCore::ConfigItem.new(key: :token,
                                     env_name: "TELEGRAM_TOKEN",
                                  description: "A unique authentication token given when a bot is created",
                                     optional: false,
                                         type: String),
             FastlaneCore::ConfigItem.new(key: :chat_id,
                                     env_name: "TELEGRAM_CHAT_ID",
                                  description: "Unique identifier for the target chat (not in the format @channel). For getting chat id you can send any message to your bot and get chat id from response https://api.telegram.org/botYOUR_TOKEN/getupdates",
                                     optional: false,
                                         type: String),
             FastlaneCore::ConfigItem.new(key: :topic_id,
                                     env_name: "TELEGRAM_TOPIC_ID",
                                  description: "Optional unique identifier for the target topic to which the message should be sent",
                                     optional: true,
                                         type: String),
             FastlaneCore::ConfigItem.new(key: :text,
                                     env_name: "TELEGRAM_TEXT",
                                  description: "Text of the message to be sent",
                                     optional: false,
                                         type: String),
             FastlaneCore::ConfigItem.new(key: :file,
                                     env_name: "TELEGRAM_FILE",
                                   description: "File path to the file to be sent",
                                       optional: true,
                                           type: String),
             FastlaneCore::ConfigItem.new(key: :mime_type,
                                     env_name: "TELEGRAM_FILE_MIME_TYPE",
                                   description: "Mime type of file to be sent",
                                       optional: true,
                                           type: String),
             FastlaneCore::ConfigItem.new(key: :parse_mode,
                                     env_name: "TELEGRAM_PARSE_MODE",
                                  description: "Param (Markdown / HTML) for using markdown or HTML support in message",
                                     optional: true,
                                         type: String),
             FastlaneCore::ConfigItem.new(key: :proxy,
                                     env_name: "TELEGRAM_PROXY",
                                  description: "Proxy URL to be used in network requests. Example: (https://123.45.67.89:80)",
                                     optional: true,
                                         type: String)
          ]
end

.descriptionObject



64
65
66
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 64

def self.description
  "Allows post messages to telegram channel"
end

.detailsObject



76
77
78
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 76

def self.details
  "Allows post messages to telegram channel"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 125

def self.is_supported?(platform)
  true
end

.return_valueObject



72
73
74
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 72

def self.return_value
  response
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastlane/plugin/teletopic/actions/teletopic_action.rb', line 4

def self.run(params)
  UI.message("Sending message to a telegram channel")

  token = params[:token]
  chat_id = params[:chat_id]
  topic_id = params[:topic_id]
  text = params[:text]
  parse_mode = params[:parse_mode]
  file_path = params[:file]
  mime_type = params[:mime_type]

  file = nil
  if file_path != nil 
    if File.exist?(file_path)
      if mime_type == nil 
        UI.user_error!("The mime type, required for send file")
      end

      file = UploadIO.new(file_path, mime_type)
    end
  end

  if file_path != nil && file == nil 
    UI.message("Can't find file on path location")
  end

  method = (file == nil ? "sendMessage" : "sendDocument")
  uri = URI.parse("https://api.telegram.org/bot#{token}/#{method}")
  
  http = Net::HTTP.new(uri.host, uri.port)
  if params[:proxy]
    proxy_uri = URI.parse(params[:proxy])
    http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
  end
  http.use_ssl = true

  require 'net/http/post/multipart'
  text_parameter = (file == nil ? "text" : "caption")
  # request = Net::HTTP::Post::Multipart.new(uri, 
  # { 
  #   "chat_id" => chat_id,
  #   text_parameter => text,
  #   "parse_mode" => parse_mode,
  #   "document" => file
  # })

  # response = http.request(request)
  request_params = {
    "chat_id" => chat_id,
    text_parameter => text,
    "parse_mode" => parse_mode
  }

  request_params["message_thread_id"] = topic_id if topic_id
  request_params["document"] = file unless file.nil?

  request = Net::HTTP::Post::Multipart.new(uri, request_params)
  response = http.request(request)
end