|
import vibe.d; |
|
|
|
import std.stdio, |
|
std.conv, |
|
std.json; |
|
import std.net.curl; |
|
|
|
enum LINE_CHANNEL_ID = "YOUR CHANNNEL ID", |
|
LINE_CHANNEL_SECRET = "YOUR CHANNNEL SECRET", |
|
LINE_CHANNEL_MID = "YOUR CHANNNEL MID"; |
|
|
|
void sendRequestWithJson(string sendJSON) { |
|
enum serverURL = "https://trialbot-api.line.me/v1/events"; |
|
|
|
auto http = HTTP(); |
|
foreach (key, value; |
|
[ |
|
"Content-Type" : "application/json; charset=UTF-8", |
|
"X-Line-ChannelID" : LINE_CHANNEL_ID, |
|
"X-Line-ChannelSecret" : LINE_CHANNEL_SECRET, |
|
"X-Line-Trusted-User-With-ACL" : LINE_CHANNEL_MID |
|
]) { |
|
http.addRequestHeader(key, value); |
|
} |
|
|
|
writeln("send -> ", sendJSON); |
|
post(serverURL, sendJSON, http); |
|
} |
|
|
|
void sendMessageTo(string to, string message) { |
|
writeln("to -> ", to); |
|
writeln("messagee -> ", message); |
|
string sendJSON = `{"to": [` ~ "\"" ~ to ~ "\"" ~ `], "toChannel": 1383378250, "eventType": "138311608800106203", "content":{"contentType": 1, "toType": 1, "text": "` ~ message ~ `" }}`; |
|
|
|
sendRequestWithJson(sendJSON); |
|
} |
|
|
|
void botProc(HTTPServerRequest req, HTTPServerResponse res) { |
|
writeln("Receive!"); |
|
foreach (result; parseJSON(req.json.to!string).object["result"].array) { |
|
auto content = result.object["content"]; |
|
string from = content.object["from"].str; |
|
string text = content.object["text"].str; |
|
|
|
sendMessageTo(from, "さーばーえこーにゃん! : " ~ text); |
|
// or : sendMessageTo(from, "server echo : " ~ text); |
|
} |
|
} |
|
|
|
shared static this() { |
|
auto router = new URLRouter; |
|
router.post("/", &botProc); |
|
|
|
auto settings = new HTTPServerSettings; |
|
settings.port = 3000; |
|
listenHTTP(settings, router); |
|
|
|
logInfo("Boot!"); |
|
} |
|
|