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
require 'sinatra' | |
require 'bundler/setup' | |
require 'json' | |
require 'line/bot' | |
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'sqlite3://localhost/appdb') | |
class User < ActiveRecord::Base; end | |
def client | |
@client ||= LINE::Bot::Client.new { |config| |
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
{ | |
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", | |
"type": "follow", | |
"timestamp": 1462629479859, | |
"source": | |
{ | |
"type": "user", | |
"userId": "U4af4980629..." | |
} | |
} |
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
post '/callback' do #POSTを受けるURL(後述) | |
body = request.body.read #上記のjsonを読み込む | |
events = client.parse_events_from(body) | |
events.each { |event| | |
case event | |
when Line::Bot::Event::Follow #フォローイベント | |
userid = event['source']['userId'] #userId取得 | |
user = User.new | |
user.UserId = userid | |
user.save |
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
post '/send' do #外部からPOST通信を受ける | |
users = User.all #User…UserIdを保存しているDB | |
users.each{ |user| #DBに登録されている全てのユーザに | |
message = {type: 'text', text: params[:msg]} #POSTのパラメータにメッセージを乗せる | |
client.push_message(user.userId, message) #PUSH送信 | |
} | |
} | |
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
require 'net/http' | |
Net::HTTP.version_1_2 | |
http = Net::HTTP.new("<プロジェクト名>.herokuapp.com", 443) | |
http.use_ssl = true | |
response = http.post('/send', 'msg=何かメッセージ') |
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
require "twitter" | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = "<your consumer key>" | |
config.consumer_secret = "<your consumer secret>" | |
config.access_token = "<your access token>" | |
config.access_token_secret = "<your access token asecret>" | |
end | |
client_streaming = Twitter::Streaming::Client.new do |config| |
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
require "twitter" | |
client_streaming = Twitter::Streaming::Client.new do |config| | |
config.consumer_key = "<your consumer key>" | |
config.consumer_secret = "<your consumer secret>" | |
config.access_token = "<your access token>" | |
config.access_token_secret = "<your access token asecret>" | |
end | |
target = client.user("<@Screen Name>") #@で始まる方の名前 |
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
function doGet(req) { | |
var output; | |
if(req.parameters.cmd == "res"){ https://~?cmd=res | |
output = HtmlService.createTemplateFromFile("result"); | |
output.name = req.parameters.name; // https://~?cmd=res&name=XXX | |
}else{ | |
output = HtmlService.createTemplateFromFile("index"); | |
} | |
return output.evaluate(); | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
</head> | |
<body> | |
<form name='form'> | |
お名前:<input id="user" name="user" /> さん <br> | |
<a id="submit" href="https://script.google.com/macros/s/xxxxx/dev?cmd=res&name=">送信</a> | |
</form> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
</head> | |
<body> | |
<?= name ?>さん <br> | |
<a href="https://script.google.com/macros/s/xxxxx/dev">戻る</a> | |
</body> | |
</html> |
OlderNewer