Gabriel Grubba 3f8b67d1c1
Feature: add chat integration reference post (#216)
* FEATURE: Add chat integration reference post

This class works similar to a post but it is not a post.

* DEV: change how `excerpt` method works

* feature: Add `send_chat_integration_message` scriptable

add `send_chat_integration_message` scriptable that uses the rules to send a message to the chat provider
add locale strings for the new scriptable
update `ChatIntegrationReferencePost` `excerpt` method
add tests for `ChatIntegrationReferencePost`

* DEV: Add `get_channel_by_name` to every provider

This makes using `trigger_notification` easier with every provider as well.

* DEV: Add `get_channel_name` to all providers

This method gets the name of the channel based on how the provider identifies it.
Updates channel_name in locales yaml
Adds migrate_tag_added_filter_to_all_providers.rb to move all existing rules to use Automation

* DEV: Add removal of old migration data

Update small action locales with strings from core

* DEV: solve review comments

* DEV: update test locale strings

* DEV: remove empty line to trigger lint

* DEV: lint applied

* DEV: Add tests for automation integration

* DEV: add rails logger for when automatio error occurs

* DEV: move migration to be SQL only

Update provider helper to use hashes instead of dot notation

* DEV: update migration with correct table names

* DEV: Update migrate_tag_added_filter_to_all_providers to use smaller SQL queries

Commented out migrate_tag_added_from_filter_to_automation.rb

* DEV: update comments in migration file

* DEV: update indentation in client.en.yml

* DEV: update with review comments

* Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

* Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

* Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

* Update spec/integration/automation_spec.rb

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

* Update lib/discourse_chat_integration/chat_integration_reference_post.rb

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

* DEV: update specs with review comments

* DEV: update typos in tests

* DEV: inlined functions for getting channel name for provider in migration

---------

Co-authored-by: Jarek Radosz <jradosz@gmail.com>
2024-09-11 10:42:52 -03:00

126 lines
4.0 KiB
Ruby

# frozen_string_literal: true
module DiscourseChatIntegration
module Provider
module TelegramProvider
PROVIDER_NAME = "telegram".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled
CHANNEL_IDENTIFIER_KEY = "name".freeze
CHANNEL_PARAMETERS = [
{ key: "name", regex: '^\S+' },
{ key: "chat_id", regex: '^(-?[0-9]+|@\S+)$', unique: true },
]
def self.setup_webhook
newSecret = SecureRandom.hex
SiteSetting.chat_integration_telegram_secret = newSecret
message = { url: Discourse.base_url + "/chat-integration/telegram/command/" + newSecret }
response = self.do_api_request("setWebhook", message)
if response["ok"] != true
# If setting up webhook failed, disable provider
SiteSetting.chat_integration_telegram_enabled = false
Rails.logger.error(
"Failed to setup telegram webhook. Message data= " + message.to_json + " response=" +
response.to_json,
)
end
end
def self.sendMessage(message)
self.do_api_request("sendMessage", message)
end
def self.do_api_request(methodName, message)
http = FinalDestination::HTTP.new("api.telegram.org", 443)
http.use_ssl = true
access_token = SiteSetting.chat_integration_telegram_access_token
uri = URI("https://api.telegram.org/bot#{access_token}/#{methodName}")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req.body = message.to_json
response = http.request(req)
responseData = JSON.parse(response.body)
responseData
end
def self.message_text(post)
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic
category = ""
if topic.category
category =
(
if (topic.category.parent_category)
"[#{topic.category.parent_category.name}/#{topic.category.name}]"
else
"[#{topic.category.name}]"
end
)
end
tags = ""
tags = topic.tags.map(&:name).join(", ") if topic.tags.present?
I18n.t(
"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,
),
)
end
def self.trigger_notification(post, channel, rule)
chat_id = channel.data["chat_id"]
message = {
chat_id: chat_id,
text: message_text(post),
parse_mode: "html",
disable_web_page_preview: true,
}
response = sendMessage(message)
if response["ok"] != true
error_key = nil
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"
end
raise ::DiscourseChatIntegration::ProviderError.new info: {
error_key: error_key,
message: message,
response_body: response,
}
end
end
def self.get_channel_by_name(name)
DiscourseChatIntegration::Channel
.with_provider(PROVIDER_NAME)
.with_data_value(CHANNEL_IDENTIFIER_KEY, name)
.first
end
end
end
end
require_relative "telegram_command_controller.rb"