1
2
3
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
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
88
89
90
91
92
93
94
95
96
|
# frozen_string_literal: true
module Integrations
class Telegram < BaseChatNotification
TELEGRAM_HOSTNAME = "https://api.telegram.org/bot%{token}/sendMessage"
field :token,
section: SECTION_TYPE_CONNECTION,
help: -> { s_('TelegramIntegration|Unique authentication token.') },
non_empty_password_title: -> { s_('TelegramIntegration|New token') },
non_empty_password_help: -> { s_('TelegramIntegration|Leave blank to use your current token.') },
placeholder: '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11',
exposes_secrets: true,
is_secret: true,
required: true
field :room,
title: 'Channel identifier',
section: SECTION_TYPE_CONFIGURATION,
help: "Unique identifier for the target chat or the username of the target channel (format: @channelusername)",
placeholder: '@channelusername',
required: true
field :notify_only_broken_pipelines,
type: :checkbox,
section: SECTION_TYPE_CONFIGURATION,
help: 'If selected, successful pipelines do not trigger a notification event.'
field :branches_to_be_notified,
type: :select,
section: SECTION_TYPE_CONFIGURATION,
title: -> { s_('Integrations|Branches for which notifications are to be sent') },
choices: -> { branch_choices }
with_options if: :activated? do
validates :token, :room, presence: true
end
before_validation :set_webhook
def title
'Telegram'
end
def description
s_("TelegramIntegration|Send notifications about project events to Telegram.")
end
def self.to_param
'telegram'
end
def help
docs_link = ActionController::Base.helpers.link_to(
_('Learn more.'),
Rails.application.routes.url_helpers.help_page_url('user/project/integrations/telegram'),
target: '_blank',
rel: 'noopener noreferrer'
)
format(s_("TelegramIntegration|Send notifications about project events to Telegram. %{docs_link}"),
docs_link: docs_link.html_safe
)
end
def self.supported_events
super - ['deployment']
end
def avatar_url
ActionController::Base.helpers.image_path('illustrations/third-party-logos/integrations-logos/telegram.svg')
end
private
def set_webhook
self.webhook = format(TELEGRAM_HOSTNAME, token: token) if token.present?
end
def notify(message, _opts)
body = {
text: message.summary,
chat_id: room,
parse_mode: 'markdown'
}
header = { 'Content-Type' => 'application/json' }
response = Gitlab::HTTP.post(webhook, headers: header, body: Gitlab::Json.dump(body))
response if response.success?
end
def custom_data(data)
super(data).merge(markdown: true)
end
end
end
|