Skip to content

Instantly share code, notes, and snippets.

@epistrephein
Created October 4, 2016 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epistrephein/41c23b4e8c98df9fcec1276b4beaefd0 to your computer and use it in GitHub Desktop.
Save epistrephein/41c23b4e8c98df9fcec1276b4beaefd0 to your computer and use it in GitHub Desktop.
MyItasaBot
require 'telegram/bot'
require 'feedjira'
require 'json'
require 'logger'
BOTNAME = 'MyItasaBot'
BOTUSER = 'myitasabot'
VERSION = '1.0.0'
TOKEN = '__REDACTED__'
SELFDIR = File.expand_path(File.dirname(__FILE__))
FEEDDIR = File.join(SELFDIR, 'feeds')
# initialize bot
bot = Telegram::Bot::Client.new(TOKEN)
# load/create feed database
feeddb_json = File.join(SELFDIR, 'feeddb.json')
File.write(feeddb_json, JSON.generate({})) unless File.file?(feeddb_json)
feeddb = JSON.parse(File.read(feeddb_json))
feeddb.each do |id, feed|
# load user's feed db
entries = File.join(FEEDDIR, id + '.json')
if File.file?(entries)
first_run = false
db = JSON.parse(File.read(entries))
else
first_run = true
db = []
end
# get new elements
rss = Feedjira::Feed.fetch_and_parse(feed)
news = rss.entries.reject { |e| db.include?(e.title) }
news.each { |n| db << n.title }
File.write(entries, JSON.pretty_generate(db))
# clear array if first run
news = [] if first_run
# send message
news.each do |n|
bot.api.send_message(
chat_id: id.to_i,
text: n.title
)
end
end
require 'telegram/bot'
require 'json'
require 'logger'
BOTNAME = 'MyItasaBot'
BOTUSER = 'myitasabot'
VERSION = '1.0.0'
TOKEN = '__REDACTED__'
SELFDIR = File.expand_path(File.dirname(__FILE__))
FEEDDIR = File.join(SELFDIR, 'feeds')
# load/create log file
logger = Logger.new(File.join(SELFDIR, BOTUSER + '-listener' + '.log'))
logger.level = Logger::INFO
# load/create databases
feeddb_json = File.join(SELFDIR, 'feeddb.json')
File.write(feeddb_json, JSON.generate({})) unless File.file?(feeddb_json)
feeddb = JSON.parse(File.read(feeddb_json))
Telegram::Bot::Client.run(TOKEN) do |bot|
bot.listen do |message|
case message.text.chomp
# -- /START --
when %r{^(/start)}
logger.info('user join') { message.chat.to_h }
# greet user
bot.api.send_message(
chat_id: message.chat.id,
text:
"Ciao, #{message.from.first_name}!\n" \
'Usa /myitasa <feed url> per attivare gli aggiornamenti del tuo myItasa.'
)
# -- /STOP --
when %r{^(/stop)}
logger.info('user leave') { message.chat.to_h }
# delete user from feeddb
if feeddb.keys.include?(message.chat.id.to_s)
feeddb.delete(message.chat.id.to_s)
File.write(feeddb_json, JSON.pretty_generate(feeddb))
end
# delete user feed
user_feed_file = File.join(FEEDDIR, message.chat.id.to_s + '.json')
File.delete(user_feed_file) if File.file?(user_feed_file)
# farewell user
bot.api.send_message(
chat_id: message.chat.id,
text:
"Addio, #{message.from.first_name}.\n" \
'Non riceverai più gli aggiornamenti del tuo myItasa.'
)
# -- /MYITASA --
when %r{^(/myitasa)$}
bot.api.send_message(
chat_id: message.chat.id,
text: 'Usa /myitasa <feed url> per attivare gli aggiornamenti del tuo myItasa.'
)
# -- /MYITASA <FEED> --
when %r{^(/myitasa) .*}
# get feed url
feed_url = message.text.gsub(%r{/myitasa +}, '')
# check feed and add to db
if feed_url['italiansubs.net/index.php?option=com_rsssub']
feeddb[message.chat.id.to_s] = feed_url
File.write(feeddb_json, JSON.pretty_generate(feeddb))
logger.info('new feed') { "#{message.chat.id}: #{feed_url}" }
# notify user - success
bot.api.send_message(
chat_id: message.chat.id,
text: 'Feed aggiunto con successo. ' \
'Gli aggiornamenti cominceranno da ora.'
)
else
# notify user - failure
bot.api.send_message(
chat_id: message.chat.id,
text: 'Questo feed non è valido.'
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment