-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3_bot_script_example.js
86 lines (79 loc) · 2.4 KB
/
3_bot_script_example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { relayInit } from "nostr-tools";
import { upsertTableOrCreate, getSingle } from "nostr-key-value";
import dotenv from "dotenv";
import { postNostr, currUnixtime } from "./util/util.js";
dotenv.config();
// 必要な設定をしていきます。
const relayUrl = "wss://relay-jp.nostr.wirednet.jp";
const nsec = process.env.NSEC;
const npub = process.env.NPUB;
const tableName = "home_system";
const tableTitle = "home_system";
// Nostr 投稿文言作成
const send = (content, targetEvent = null) => {
const created = targetEvent ? targetEvent.created_at + 1 : currUnixtime();
const ev = {
kind: 1,
content: content,
tags: [],
created_at: created,
};
if (targetEvent) {
ev.tags.push(["e", targetEvent.id]);
ev.tags.push(["p", targetEvent.pubkey]);
}
postNostr(ev, nsec, relayUrl);
};
// メイン関数
const main = async () => {
const relay = relayInit(relayUrl);
relay.on("error", () => {
throw "failed to connnect";
});
await relay.connect();
const sub = relay.sub([
// { kinds: [1], "#p": [getPublicKey(NPUB)], since: currUnixtime() - 60 },
{ kinds: [1], since: currUnixtime() },
]);
// リプライ返信
sub.on("event", async (ev) => {
if (ev.content.match(/^扉の状態は?$/i)) {
// 扉の状態を取得して、返事する処理
const single = await getSingle([relayUrl], npub, tableName, "door_status");
const result = single ?? "わかんない!"
send(result, ev);
}
if (ev.content.match(/^開けて$/i)) {
// 扉を開けて、状態をDBに保存する処理
const values = [["door_status", "開いてる!"]];
const table_ev = await upsertTableOrCreate(
[relayUrl],
npub,
tableName,
tableTitle,
[],
values
);
postNostr(table_ev, nsec, relayUrl);
send("開けたよ!", ev);
}
if (ev.content.match(/^閉めて$/i)) {
// 扉を閉めて、状態をDBに保存する処理
const values = [["door_status", "閉まってる!"]];
const table_ev = await upsertTableOrCreate(
[relayUrl],
npub,
tableName,
tableTitle,
[],
values
);
postNostr(table_ev, nsec, relayUrl);
send("閉めたよ!!", ev);
}
if (ev.content.match(/^光あれ$/i)) {
send("世界は光に包まれた", ev);
}
});
};
main().catch((e) => console.error(e));