No Description

response-bot.rb 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. =begin
  2. This file is part of GSRuby.
  3. GSRuby is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. GSRuby is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GSRuby. If not, see <http://www.gnu.org/licenses/>.
  13. =end
  14. =begin
  15. Simple bot that checks for mentions and do something
  16. USAGE:
  17. - Edit the variables node, user and password
  18. - Edit filename, this is a plain text file used to
  19. store the ID of last notice proccesed
  20. - Add 'ssl_insecure: true' if your instance uses a
  21. self-signed cert
  22. - Finally, you can setup this script in a cron job
  23. for publish automatically, the following example checks
  24. for new mentions each 15 min
  25. $ crontab -e
  26. */15 * * * * cd /path/to/script/dir && ruby ./response-bot.rb
  27. =end
  28. require 'gsruby'
  29. node = 'https://yourinstance.tld'
  30. user = 'your_username'
  31. passwd = 'your_password'
  32. filename = 'last.txt'
  33. max_replies = 500 # Maximum number of notices for request (be careful)
  34. # Creates a new GNU Social object
  35. session = GNUSocial::Client.new(node,user,passwd, ssl: true)
  36. # Open a file for read/write the las notice that we have read
  37. if File.exist?(filename)
  38. file = File.open(filename,'r+')
  39. else
  40. file = File.open(filename,'w+')
  41. end
  42. # Try to read, if fails continues without last notice
  43. begin
  44. last = file.readline
  45. rescue EOFError
  46. last = nil
  47. end
  48. # Request unread notices and do something with it
  49. session.mentions(since: last, count: max_replies).notices do |notice|
  50. # Delete the following line and do something more cool
  51. session.reply("Received: #{notice.text}", notice)
  52. end
  53. # Update the file with the last notice proccesed
  54. file.rewind
  55. file.write(session.mentions.newest_notice.id)
  56. file.close