-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.rb
79 lines (71 loc) · 2.04 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
require 'telegram/bot'
require 'yaml'
require 'date'
require File.expand_path './text'
require 'uri'
class TelegramBot
attr_reader :bot
def initialize(token, options)
@bot = Telegram::Bot::Client.new(token, options)
end
def cycle
bot.fetch_updates do |message|
case message
when Telegram::Bot::Types::Message
handle_message message
end
end
end
def reply(message, options)
return if message.nil? && options[:chat_id].nil?
opt = { chat_id: message.respond_to?('chat') ? message.chat.id : nil, text: '' }
opt.merge! options
bot.api.send_message(opt)
end
def handle_message(message)
case message.text
when '/start'
reply message, text: Text["welcome"]
when /\/configure/
slack = /\/configure (.+)/.match(message.text).captures
slack = slack.first.split(' ').first
if slack
use_registry do |reg|
reg[slack] = (reg[slack] || []) << message.chat.id
end
reply message, text: "Configured slack token '#{slack}' to be used in this chat"
else
reply message, text: "Usage: /configure <slack token>, for more information, use /help"
end
when /\/clear/
deleted = true
text = "This chat does not have any assigned slack tokens, for more informations, use /help"
use_registry do |reg|
reg.each do |tok, chats|
chats.delete(message.chat.id) { deleted = false }
text = "Successfully removed '#{tok}' from this chat" if deleted
end
end
reply message, text: text
end
end
def handle_slack_params(params)
use_registry do |reg|
tok = params[:token]
if reg[tok]
reg[tok].each do |chat|
reply(nil, chat_id: chat, text: "got slack message in channel #{params[:channel_name]}\nat #{Time.at(params[:timestamp].to_i)}\n#{params[:user_name]}: #{URI.decode(params[:text]).gsub('+', ' ')}")
end
else
puts "didnt find token #{tok.inspect}"
end
end
end
def use_registry
reg = YAML.load_file(CONST::Telegram::Registry_File)
reg = {} unless reg
ret = yield reg
File.open(CONST::Telegram::Registry_File, 'w') { |f| f.puts reg.to_yaml }
ret
end
end