Class: Elephrame::Bots::BaseBot
- Inherits:
-
Object
- Object
- Elephrame::Bots::BaseBot
- Defined in:
- lib/elephrame/bot.rb
Overview
a superclass for other bots holds common methods and variables
Direct Known Subclasses
Command, GenerativeBot, Interact, PeriodInteract, Periodic, Reply, TraceryBot, Watcher
Constant Summary collapse
- NoBotRegex =
/#?NoBot/i
- FNGabLink =
'https://fediverse.network/mastodon?build=gab'
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#failed ⇒ Object
readonly
Returns the value of attribute failed.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#strip_html ⇒ Object
Returns the value of attribute strip_html.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.backup_method(method, new_name) ⇒ Object
A helper method that is a wrapper around alias_method (just to make some code easier to read).
Instance Method Summary collapse
-
#fetch_account_id(account_name) ⇒ String
Gets the ID of an account given the user’s handle or username.
-
#fetch_list_id(name) ⇒ Integer
Gets the ID of a list given the name.
-
#find_ancestor(id, depth = 10, stop_at = 1) ⇒ Mastodon::Status
Finds most recent post by bot in the ancestors of a provided post.
-
#initialize ⇒ Elephrame::Bots::BaseBot
constructor
Sets up our REST
client
, gets and saves ourusername
, sets default value forstrip_html
(true), andmax_retries
(5),failed
. -
#no_bot?(account_id) ⇒ Bool
Checks to see if a user has some form of “#NoBot” in their bio or in their profile fields (so we can make making friendly bots easier!).
-
#post(text, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: []) ⇒ Object
Creates a post, uploading media if need be.
Constructor Details
#initialize ⇒ Elephrame::Bots::BaseBot
Sets up our REST client
, gets and saves our username
, sets default value for strip_html
(true), and max_retries
(5), failed
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/elephrame/bot.rb', line 25 def initialize raise "Fuck off Gabber" if Net::HTTP.get(URI.parse(FNGabLink)).include? URI.parse(ENV['INSTANCE']).host @client = Mastodon::REST::Client.new(base_url: ENV['INSTANCE'], bearer_token: ENV['TOKEN']) @username = @client.verify_credentials().acct @strip_html = true @max_retries = 5 @failed = { media: false, post: false } end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/elephrame/bot.rb', line 13 def client @client end |
#failed ⇒ Object (readonly)
Returns the value of attribute failed.
13 14 15 |
# File 'lib/elephrame/bot.rb', line 13 def failed @failed end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
14 15 16 |
# File 'lib/elephrame/bot.rb', line 14 def max_retries @max_retries end |
#strip_html ⇒ Object
Returns the value of attribute strip_html.
14 15 16 |
# File 'lib/elephrame/bot.rb', line 14 def strip_html @strip_html end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
13 14 15 |
# File 'lib/elephrame/bot.rb', line 13 def username @username end |
Class Method Details
.backup_method(method, new_name) ⇒ Object
A helper method that is a wrapper around alias_method (just to make some code easier to read)
157 158 159 |
# File 'lib/elephrame/bot.rb', line 157 def self.backup_method(method, new_name) alias_method new_name, method end |
Instance Method Details
#fetch_account_id(account_name) ⇒ String
Gets the ID of an account given the user’s handle or username
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/elephrame/bot.rb', line 137 def fetch_account_id(account_name) name = account_name.reverse.chomp('@').reverse search = @client.search("@#{name}") accounts = {} search.accounts.each do |acct| accounts[acct.acct] = acct.id accounts[acct.username] = acct.id end accounts[name] end |
#fetch_list_id(name) ⇒ Integer
Gets the ID of a list given the name
122 123 124 125 126 127 128 |
# File 'lib/elephrame/bot.rb', line 122 def fetch_list_id(name) lists = {} @client.lists.collect do |l| lists[l.title] = l.id end lists[name] end |
#find_ancestor(id, depth = 10, stop_at = 1) ⇒ Mastodon::Status
Finds most recent post by bot in the ancestors of a provided post
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/elephrame/bot.rb', line 88 def find_ancestor(id, depth = 10, stop_at = 1) depth.times { post = @client.status(id) unless id.nil? id = post.in_reply_to_id stop_at -= 1 if post.account.acct == @username return post if stop_at.zero? } return nil end |
#no_bot?(account_id) ⇒ Bool
Checks to see if a user has some form of “#NoBot” in their bio or in their profile fields (so we can make making friendly bots easier!)
109 110 111 112 113 |
# File 'lib/elephrame/bot.rb', line 109 def no_bot? account_id acct = @client.account(account_id) acct.note =~ NoBotRegex || acct.fields.any? {|f| f.name =~ NoBotRegex || f.value =~ NoBotRegex} end |
#post(text, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: []) ⇒ Object
Creates a post, uploading media if need be
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 |
# File 'lib/elephrame/bot.rb', line 47 def post(text, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: []) uploaded_ids = [] unless media.size.zero? @failed[:media] = retry_if_needed { uploaded_ids = media.collect {|m| @client.upload_media(m).id } } end = { visibility: visibility, spoiler_text: spoiler, in_reply_to_id: reply_id, media_ids: @failed[:media] ? [] : uploaded_ids, sensitive: hide_media } # create a unique key for the status # this key prevents a status from being posted more than once # SHOULD be unique enough, by smooshing time and content together idempotency = Digest::SHA256.hexdigest("#{Time.now}#{text}") @failed[:post] = retry_if_needed { [:headers] = { "Idempotency-Key" => idempotency } @client.create_status text, } end |