123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- =begin
- This file is part of GSRuby.
- GSRuby is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- GSRuby is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GSRuby. If not, see <http://www.gnu.org/licenses/>.
- =end
- =begin
- Simple bot that checks for mentions and do something
- USAGE:
- - Edit the variables node, user and password
- - Edit filename, this is a plain text file used to
- store the ID of last notice proccesed
- - Add 'ssl_insecure: true' if your instance uses a
- self-signed cert
- - Finally, you can setup this script in a cron job
- for publish automatically, the following example checks
- for new mentions each 15 min
- $ crontab -e
- */15 * * * * cd /path/to/script/dir && ruby ./response-bot.rb
- =end
- require 'gsruby'
- node = 'https://yourinstance.tld'
- user = 'your_username'
- passwd = 'your_password'
- filename = 'last.txt'
- max_replies = 500 # Maximum number of notices for request (be careful)
- # Creates a new GNU Social object
- session = GNUSocial::Client.new(node,user,passwd, ssl: true)
- # Open a file for read/write the las notice that we have read
- if File.exist?(filename)
- file = File.open(filename,'r+')
- else
- file = File.open(filename,'w+')
- end
- # Try to read, if fails continues without last notice
- begin
- last = file.readline
- rescue EOFError
- last = nil
- end
- # Request unread notices and do something with it
- session.mentions(since: last, count: max_replies).notices do |notice|
- # Delete the following line and do something more cool
- session.reply("Received: #{notice.text}", notice)
- end
- # Update the file with the last notice proccesed
- file.rewind
- file.write(session.mentions.newest_notice.id)
- file.close
|