| 
									
										
										
										
											2019-05-13 10:37:49 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  | module DiscourseChatIntegration | 
					
						
							| 
									
										
										
										
											2017-06-27 19:21:27 +01:00
										 |  |  |   module Provider | 
					
						
							|  |  |  |     module TelegramProvider | 
					
						
							|  |  |  |       PROVIDER_NAME = "telegram".freeze | 
					
						
							| 
									
										
										
										
											2017-07-03 15:53:26 +01:00
										 |  |  |       PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |       CHANNEL_PARAMETERS = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         { key: "name", regex: '^\S+' }, | 
					
						
							|  |  |  |         { key: "chat_id", regex: '^(-?[0-9]+|@\S+)$', unique: true }, | 
					
						
							|  |  |  |       ] | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |       def self.setup_webhook | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         newSecret = SecureRandom.hex | 
					
						
							|  |  |  |         SiteSetting.chat_integration_telegram_secret = newSecret | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         message = { url: Discourse.base_url + "/chat-integration/telegram/command/" + newSecret } | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         response = self.do_api_request("setWebhook", message) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         if response["ok"] != true | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |           # If setting up webhook failed, disable provider | 
					
						
							|  |  |  |           SiteSetting.chat_integration_telegram_enabled = false | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |           Rails.logger.error( | 
					
						
							|  |  |  |             "Failed to setup telegram webhook. Message data= " + message.to_json + " response=" + | 
					
						
							|  |  |  |               response.to_json, | 
					
						
							|  |  |  |           ) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.sendMessage(message) | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         self.do_api_request("sendMessage", message) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.do_api_request(methodName, message) | 
					
						
							| 
									
										
										
										
											2022-11-01 17:36:56 +00:00
										 |  |  |         http = FinalDestination::HTTP.new("api.telegram.org", 443) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         http.use_ssl = true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         access_token = SiteSetting.chat_integration_telegram_access_token | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         uri = URI("https://api.telegram.org/bot#{access_token}/#{methodName}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json") | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         req.body = message.to_json | 
					
						
							|  |  |  |         response = http.request(req) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 21:08:28 +02:00
										 |  |  |         responseData = JSON.parse(response.body) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-14 15:03:49 -05:00
										 |  |  |         responseData | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.message_text(post) | 
					
						
							| 
									
										
										
										
											2022-05-30 17:13:55 +01:00
										 |  |  |         display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         topic = post.topic | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         category = "" | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         if topic.category | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |           category = | 
					
						
							|  |  |  |             ( | 
					
						
							|  |  |  |               if (topic.category.parent_category) | 
					
						
							|  |  |  |                 "[#{topic.category.parent_category.name}/#{topic.category.name}]" | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 "[#{topic.category.name}]" | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         tags = "" | 
					
						
							|  |  |  |         tags = topic.tags.map(&:name).join(", ") if topic.tags.present? | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-14 15:03:49 -05:00
										 |  |  |         I18n.t( | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |           "chat_integration.provider.telegram.message", | 
					
						
							|  |  |  |           user: display_name, | 
					
						
							|  |  |  |           post_url: post.full_url, | 
					
						
							|  |  |  |           title: CGI.escapeHTML(topic.title), | 
					
						
							|  |  |  |           post_excerpt: | 
					
						
							|  |  |  |             post.excerpt( | 
					
						
							|  |  |  |               SiteSetting.chat_integration_telegram_excerpt_length, | 
					
						
							|  |  |  |               text_entities: true, | 
					
						
							|  |  |  |               strip_links: true, | 
					
						
							|  |  |  |               remap_emoji: true, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-15 11:45:25 -04:00
										 |  |  |       def self.trigger_notification(post, channel, rule) | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         chat_id = channel.data["chat_id"] | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         message = { | 
					
						
							|  |  |  |           chat_id: chat_id, | 
					
						
							|  |  |  |           text: message_text(post), | 
					
						
							|  |  |  |           parse_mode: "html", | 
					
						
							|  |  |  |           disable_web_page_preview: true, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         response = sendMessage(message) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         if response["ok"] != true | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |           error_key = nil | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |           if response["description"].include? "chat not found" | 
					
						
							|  |  |  |             error_key = "chat_integration.provider.telegram.errors.channel_not_found" | 
					
						
							|  |  |  |           elsif response["description"].include? "Forbidden" | 
					
						
							|  |  |  |             error_key = "chat_integration.provider.telegram.errors.forbidden" | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |           raise ::DiscourseChatIntegration::ProviderError.new info: { | 
					
						
							|  |  |  |                                                                 error_key: error_key, | 
					
						
							|  |  |  |                                                                 message: message, | 
					
						
							|  |  |  |                                                                 response_body: response, | 
					
						
							|  |  |  |                                                               } | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-06-27 19:21:27 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-07-05 15:03:02 +01:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-19 16:28:02 +01:00
										 |  |  | require_relative "telegram_command_controller.rb" |