Class: Fastlane::Actions::TelegramAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



66
67
68
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 66

def self.authors
  ['Maxim Feduishkin']
end

.available_optionsObject



78
79
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 78

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :token,
      env_name: 'TELEGRAM_TOKEN',
      description: 'Bot authentication token',
      optional: false,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :chat_id,
      env_name: 'TELEGRAM_CHAT_ID',
      description: 'Unique identifier for the target chat',
      optional: false,
      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: 'Parse mode: Markdown or HTML',
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :message_thread_id,
      env_name: 'TELEGRAM_MESSAGE_THREAD_ID',
      description: 'Telegram forum topic id',
      optional: true,
      type: Integer
    ),
    FastlaneCore::ConfigItem.new(
      key: :proxy,
      env_name: 'TELEGRAM_PROXY',
      description: 'Proxy URL for network requests',
      optional: true,
      type: String
    )
  ]
end

.descriptionObject



62
63
64
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 62

def self.description
  'Allows posting messages to Telegram chats'
end

.detailsObject



74
75
76
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 74

def self.details
  'Allows posting messages and documents to Telegram chats'
end

.is_supported?(_platform) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 139

def self.is_supported?(_platform)
  true
end

.return_valueObject



70
71
72
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 70

def self.return_value
  'Telegram API 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
# File 'lib/fastlane/plugin/telegram_plus/actions/telegram_action.rb', line 4

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

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

  file = nil
  unless 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_params = {
    'chat_id' => chat_id,
    text_parameter => text,
    'parse_mode' => parse_mode,
    'document' => file
  }
  unless message_thread_id.nil?
    request_params['message_thread_id'] = message_thread_id.to_s
  end

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

  http.request(request)
end