-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39430ba
commit f0cb197
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,6 @@ dokuwiki: | |
|
||
playstore: | ||
jsonfile: file.json | ||
|
||
twitter_hooks: | ||
secret: SomethingSecret |