Skip to content

Instantly share code, notes, and snippets.

@gyosit
gyosit / app.rb
Last active March 25, 2018 09:27
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|
{
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"type": "follow",
"timestamp": 1462629479859,
"source":
{
"type": "user",
"userId": "U4af4980629..."
}
}
@gyosit
gyosit / app.rb
Last active March 25, 2018 09:27
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
@gyosit
gyosit / app.rb
Last active March 26, 2018 07:53
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
require 'net/http'
Net::HTTP.version_1_2
http = Net::HTTP.new("<プロジェクト名>.herokuapp.com", 443)
http.use_ssl = true
response = http.post('/send', 'msg=何かメッセージ')
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|
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>") #@で始まる方の名前
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();
}
<!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>
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<?= name ?>さん <br>
<a href="https://script.google.com/macros/s/xxxxx/dev">戻る</a>
</body>
</html>