Skip to content

Commit

Permalink
Add Twitter mention/follower plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
carstene1ns committed Jul 29, 2018
1 parent 39430ba commit f0cb197
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions EV0002.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require_relative "plugins/dokuwiki_xmlrpc"
require_relative "plugins/blog_webhooks"
require_relative "plugins/playstore_reviews"
require_relative "plugins/twitter_webhooks"

PWD = File.dirname(File.expand_path(__FILE__))

Expand Down Expand Up @@ -43,6 +44,7 @@
Cinch::DokuwikiXMLRPC,
Cinch::BlogWebhooks,
Cinch::PlayStoreReviews,
Cinch::TwitterWebhooks,
]
end

Expand Down Expand Up @@ -100,6 +102,10 @@
:json_key => PWD + "/" + $secrets["playstore"]["jsonfile"]
}

config.plugins.options[Cinch::TwitterWebhooks] = {
:secret => $secrets["twitter_hooks"]["secret"]
}

# log to file
file = File.open("#{PWD}/data/bot.log", "a")
file.sync = true
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ https://github.com/Quintus/cinch-plugins
Uses the Google PlayStore API (pulled with a timer) to provide channel notifications,
when someone adds a new review or updates an old review for our Android app.

* plugins/twitter_webhooks.rb:

Uses Zapier webhooks to provide channel notifications, when something project related
happens on Twitter. This uses the webserver provided by http_server.rb and
relies on the Zapier service and Twitter api.

[webchat]: https://kiwiirc.com/nextclient/#ircs://irc.freenode.net/#easyrpg?nick=rpgguest??
[cinch]: https://github.com/cinchrb/cinch
[ev0001]: https://github.com/EasyRPG/EV0001
Expand Down
87 changes: 87 additions & 0 deletions plugins/twitter_webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#
# This cinch plugin is part of EV0002
#
# written by carstene1ns <dev @ f4ke . de> 2018
# available under ISC license
#

require "json"

class Cinch::TwitterWebhooks
include Cinch::Plugin
extend Cinch::HttpServer::Verbs

post "/twitter_webhook" do
request.body.rewind
payload = request.body.read

# check X-Zapier-Token, to ensure authorization
secret = bot.config.plugins.options[Cinch::TwitterWebhooks][:secret]
halt 403 unless Rack::Utils.secure_compare(secret, request.env['HTTP_X_ZAPIER_TOKEN'])

# return if we got an x-www-form-urlencoded request
halt 400 if params[:payload]

# return if we got no valid json data
data = JSON.parse(payload)
halt 400 if data.empty?

# get event type
event = data["type"]

# get user info
user = data["user"]

# handle event
case event
when "follower"

template = "New follower: %s (https://twitter.com/%s)! \x0308🎉\x0F"
message = sprintf(template,
data["name"],
user)

when "mention"

template = "New tweet by %s (https://twitter.com/%s/status/%s):"
message = sprintf(template,
data["name"],
user,
data["id"])

# add up to 200 characters of the tweet, sans all whitespace
tweet = data["tweet"].gsub(/\s+/,' ').strip
message << "\n> " + tweet[0, 200]
message << "…" if tweet.length > 200

else
# something we do not know, yet
info "Error: Unknown Twitter event '#{event}'! :[]"
204
end

# output
bot.channels[0].send("[Twitter] #{message}")

204
end

# ignore GET requests
get "/twitter_webhook" do
204
end

# error on unsupported requests
put "/twitter_webhook" do
400
end

delete "/twitter_webhook" do
400
end

patch "/twitter_webhook" do
400
end

end
3 changes: 3 additions & 0 deletions template.secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ dokuwiki:

playstore:
jsonfile: file.json

twitter_hooks:
secret: SomethingSecret

0 comments on commit f0cb197

Please sign in to comment.