mirror of
https://github.com/discourse/discourse-chat-integration.git
synced 2025-02-16 16:34:50 +00:00
Add Telegram provider.
Does not yet support slash commands, and is without tests.
This commit is contained in:
parent
04cb6f1d73
commit
2e94f23fbe
@ -36,6 +36,7 @@ class DiscourseChat::ChatController < ApplicationController
|
||||
rescue Discourse::InvalidParameters, ActiveRecord::RecordNotFound => e
|
||||
render json: {errors: [e.message]}, status: 422
|
||||
rescue DiscourseChat::ProviderError => e
|
||||
Rails.logger.error("Test provider failed #{e.info}")
|
||||
if e.info.key?(:error_key) and !e.info[:error_key].nil?
|
||||
render json: {error_key: e.info[:error_key]}, status: 422
|
||||
else
|
||||
|
@ -50,6 +50,6 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
|
||||
|
||||
scope :with_provider, ->(provider) { where("value::json->>'provider'=?", provider)}
|
||||
|
||||
scope :with_data_value, ->(key, value) { where("(value::json->>'data')::json->>?=?", key, value)}
|
||||
scope :with_data_value, ->(key, value) { where("(value::json->>'data')::json->>?=?", key.to_s, value.to_s)}
|
||||
|
||||
end
|
@ -63,4 +63,13 @@ en:
|
||||
#######################################
|
||||
telegram:
|
||||
title: "Telegram"
|
||||
channel_instructions: "Enter a chat_id. You will need to add the bot to the chat first."
|
||||
param:
|
||||
name:
|
||||
title: "Name"
|
||||
help: "A name to describe the channel. It is not used for the connection for telegram."
|
||||
chat_id:
|
||||
title: Chat ID
|
||||
help: A number given to you by the bot, or a broadcast channel identifier in the form @channelname
|
||||
errors:
|
||||
channel_not_found: "The specified channel does not exist on Telegram"
|
||||
forbidden: "The bot does not have permission to post to this channel"
|
@ -60,4 +60,12 @@ en:
|
||||
|
||||
*List rules:* `/discourse status`
|
||||
|
||||
*Help:* `/discourse help`
|
||||
*Help:* `/discourse help`
|
||||
|
||||
telegram:
|
||||
unknown_chat: "This chat isn't setup on %{site_title}. Ask an administrator to add a channel with 'Chat ID' %{chat_id}."
|
||||
known_chat: "This chat is setup on %{site_title}. Configure it in the admin panel. (Chat ID: %{chat_id})"
|
||||
message: |-
|
||||
<b>%{user}</b> posted in <a href="%{post_url}">%{title}</a>
|
||||
|
||||
<pre>%{post_excerpt}</pre>
|
@ -30,3 +30,11 @@ plugins:
|
||||
#######################################
|
||||
chat_integration_telegram_enabled:
|
||||
default: false
|
||||
chat_integration_telegram_access_token:
|
||||
default: ''
|
||||
chat_integration_telegram_excerpt_length:
|
||||
default: 400
|
||||
chat_integration_telegram_enable_slash_commands:
|
||||
default: true
|
||||
chat_integration_telegram_secret:
|
||||
hidden: true
|
@ -2,9 +2,80 @@ module DiscourseChat::Provider::TelegramProvider
|
||||
class TelegramCommandController < DiscourseChat::Provider::HookController
|
||||
requires_provider ::DiscourseChat::Provider::TelegramProvider::PROVIDER_NAME
|
||||
|
||||
def say_hello
|
||||
before_filter :telegram_token_valid?, only: :command
|
||||
|
||||
render json: {hello: "from telegram"}
|
||||
skip_before_filter :check_xhr,
|
||||
:preload_json,
|
||||
:verify_authenticity_token,
|
||||
:redirect_to_login_if_required,
|
||||
only: :command
|
||||
|
||||
def command
|
||||
|
||||
# If it's a new message (telegram also sends hooks for other reasons that we don't care about)
|
||||
if params.key?('message')
|
||||
chat_id = params['message']['chat']['id']
|
||||
|
||||
message_text = process_command(params['message'])
|
||||
|
||||
message = {
|
||||
chat_id: chat_id,
|
||||
text: message_text,
|
||||
parse_mode: "html",
|
||||
disable_web_page_preview: true,
|
||||
}
|
||||
|
||||
DiscourseChat::Provider::TelegramProvider.sendMessage(message)
|
||||
|
||||
end
|
||||
|
||||
# Always give telegram a success message, otherwise we'll stop receiving webhooks
|
||||
data = {
|
||||
success: true
|
||||
}
|
||||
render json: data
|
||||
end
|
||||
|
||||
def process_command(message)
|
||||
chat_id = params['message']['chat']['id']
|
||||
|
||||
provider = DiscourseChat::Provider::TelegramProvider::PROVIDER_NAME
|
||||
|
||||
channel = DiscourseChat::Channel.with_provider(provider).with_data_value('chat_id',chat_id).first
|
||||
|
||||
if channel.nil?
|
||||
return I18n.t(
|
||||
"chat_integration.provider.telegram.unknown_chat",
|
||||
site_title: CGI::escapeHTML(SiteSetting.title),
|
||||
chat_id: chat_id,
|
||||
)
|
||||
end
|
||||
|
||||
# If slash commands disabled, send a generic message
|
||||
if !SiteSetting.chat_integration_telegram_enable_slash_commands
|
||||
return I18n.t(
|
||||
"chat_integration.provider.telegram.known_chat",
|
||||
site_title: CGI::escapeHTML(SiteSetting.title),
|
||||
chat_id: chat_id,
|
||||
)
|
||||
end
|
||||
|
||||
tokens = message['text'].split(" ")
|
||||
|
||||
return "I don't understand how to do that yet"
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
def telegram_token_valid?
|
||||
params.require(:token)
|
||||
|
||||
if SiteSetting.chat_integration_telegram_secret.blank? ||
|
||||
SiteSetting.chat_integration_telegram_secret != params[:token]
|
||||
|
||||
raise Discourse::InvalidAccess.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -14,6 +85,6 @@ module DiscourseChat::Provider::TelegramProvider
|
||||
end
|
||||
|
||||
TelegramEngine.routes.draw do
|
||||
get "command" => "telegram_command#say_hello"
|
||||
post "command/:token" => "telegram_command#command"
|
||||
end
|
||||
end
|
15
lib/discourse_chat/provider/telegram/telegram_initializer.rb
Normal file
15
lib/discourse_chat/provider/telegram/telegram_initializer.rb
Normal file
@ -0,0 +1,15 @@
|
||||
Rails.logger.error("LOADED")
|
||||
DiscourseEvent.on(:site_setting_saved) do |sitesetting|
|
||||
isEnabledSetting = sitesetting.name == 'chat_integration_telegram_enabled'
|
||||
isAccessToken = sitesetting.name == 'chat_integration_telegram_access_token'
|
||||
|
||||
if (isEnabledSetting or isAccessToken)
|
||||
enabled = isEnabledSetting ? sitesetting.value == 't' : SiteSetting.chat_integration_telegram_enabled
|
||||
# Rails.logger.error("JOB ENQUEUED"+sitesetting.value+SiteSetting.chat_integration_telegram_enabled.to_s)
|
||||
if enabled
|
||||
Scheduler::Defer.later("Setup Telegram Webhook") do
|
||||
DiscourseChat::Provider::TelegramProvider.setup_webhook()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -3,10 +3,107 @@ module DiscourseChat
|
||||
module TelegramProvider
|
||||
PROVIDER_NAME = "telegram".freeze
|
||||
PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled
|
||||
CHANNEL_PARAMETERS = []
|
||||
CHANNEL_PARAMETERS = [
|
||||
{key: "name", regex: '^\S+'},
|
||||
{key: "chat_id", regex: '^(-?[0-9]+|@\S+)$'}
|
||||
]
|
||||
|
||||
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 not 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)
|
||||
return self.do_api_request('sendMessage', message)
|
||||
end
|
||||
|
||||
def self.do_api_request(methodName, message)
|
||||
http = Net::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)
|
||||
|
||||
return responseData
|
||||
end
|
||||
|
||||
def self.message_text(post)
|
||||
display_name = "@#{post.user.username}"
|
||||
full_name = post.user.name || ""
|
||||
|
||||
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
|
||||
display_name = "#{full_name} @#{post.user.username}"
|
||||
end
|
||||
|
||||
topic = post.topic
|
||||
|
||||
category = ''
|
||||
if topic.category
|
||||
category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]": "[#{topic.category.name}]"
|
||||
end
|
||||
|
||||
tags = ''
|
||||
if topic.tags.present?
|
||||
tags = topic.tags.map(&:name).join(', ')
|
||||
end
|
||||
|
||||
return I18n.t(
|
||||
"chat_integration.provider.telegram.message",
|
||||
user: display_name,
|
||||
post_url: "https://meta.discourse.org",
|
||||
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)
|
||||
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 not response['ok'] == true
|
||||
error_key = nil
|
||||
if response['description'].include? 'chat not found'
|
||||
error_key = 'chat_integration.provider.telegram.channel_not_found'
|
||||
elsif response['description'].include? 'Forbidden'
|
||||
error_key = 'chat_integration.provider.telegram.forbidden'
|
||||
end
|
||||
raise ::DiscourseChat::ProviderError.new info: {error_key: error_key, message: message, response_body:response}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require_relative "telegram_command_controller.rb"
|
||||
require_relative "telegram_command_controller.rb"
|
||||
require_relative "telegram_initializer.rb"
|
Loading…
x
Reference in New Issue
Block a user