-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.rb
132 lines (108 loc) · 3.87 KB
/
telegram.rb
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
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
# source: app/models/channel/telegram.rb
# docker cp telegram.rb rasachatbot-sidekiq-1:/app/app/models/channel/telegram.rb
# == Schema Information
#
# Table name: channel_telegram
#
# id :bigint not null, primary key
# bot_name :string
# bot_token :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
# Indexes
#
# index_channel_telegram_on_bot_token (bot_token) UNIQUE
#
class Channel::Telegram < ApplicationRecord
include Channelable
self.table_name = 'channel_telegram'
EDITABLE_ATTRS = [:bot_token].freeze
before_validation :ensure_valid_bot_token, on: :create
validates :bot_token, presence: true, uniqueness: true
before_save :setup_telegram_webhook
def name
'Telegram'
end
def telegram_api_url
"https://api.telegram.org/bot#{bot_token}"
end
def send_message_on_telegram(message)
return send_message(message) if message.attachments.empty?
send_attachments(message)
end
def get_telegram_profile_image(user_id)
# get profile image from telegram
response = HTTParty.get("#{telegram_api_url}/getUserProfilePhotos", query: { user_id: user_id })
return nil unless response.success?
photos = response.parsed_response.dig('result', 'photos')
return if photos.blank?
get_telegram_file_path(photos.first.last['file_id'])
end
def get_telegram_file_path(file_id)
response = HTTParty.get("#{telegram_api_url}/getFile", query: { file_id: file_id })
return nil unless response.success?
"https://api.telegram.org/file/bot#{bot_token}/#{response.parsed_response['result']['file_path']}"
end
private
def ensure_valid_bot_token
response = HTTParty.get("#{telegram_api_url}/getMe")
unless response.success?
errors.add(:bot_token, 'invalid token')
return
end
self.bot_name = response.parsed_response['result']['username']
end
def setup_telegram_webhook
HTTParty.post("#{telegram_api_url}/deleteWebhook")
response = HTTParty.post("#{telegram_api_url}/setWebhook",
body: {
url: "#{ENV.fetch('FRONTEND_URL', nil)}/webhooks/telegram/#{bot_token}"
})
errors.add(:bot_token, 'error setting up the webook') unless response.success?
end
def send_message(message)
response = message_request(message.conversation[:additional_attributes]['chat_id'], message.content)
response.parsed_response['result']['message_id'] if response.success?
end
def send_attachments(message)
send_message(message) unless message.content.nil?
telegram_attachments = []
message.attachments.each do |attachment|
telegram_attachment = {}
case attachment[:file_type]
when 'audio'
telegram_attachment[:type] = 'audio'
when 'image'
telegram_attachment[:type] = 'photo'
when 'file'
telegram_attachment[:type] = 'document'
end
telegram_attachment[:media] = attachment.download_url
telegram_attachments << telegram_attachment
end
response = attachments_request(message.conversation[:additional_attributes]['chat_id'], telegram_attachments)
response.parsed_response['result'].first['message_id'] if response.success?
end
def attachments_request(chat_id, attachments)
HTTParty.post("#{telegram_api_url}/sendMediaGroup",
body: {
chat_id: chat_id,
media: attachments.to_json
})
end
def message_request(chat_id, text)
method_url = 'sendMessage'
begin
body = JSON.parse(text)
body['chat_id'] = chat_id
if body['method_url']
method_url = body['method_url']
end
rescue
body = {chat_id: chat_id, text: text}
end
HTTParty.post("#{telegram_api_url}/#{method_url}", body: body)
end
end