Skip to content

Commit

Permalink
feat: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
piloking committed Dec 31, 2024
1 parent 9f2360b commit 7235515
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 12 deletions.
125 changes: 125 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions example/browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# LINEJS in Browser

In a browser, you cannot use the Fetch API to send requests to the LINE endpoint
because of CORS.

The best option is to set a proxy server to the LINE endpoint.

However, you can get around this issue by calling LINEJS from a bookmarklet on a
page such as https://legy-jp.line-apps.com/sn. (An example of this can be found
in [browser-init.js](browser-init.js))
90 changes: 90 additions & 0 deletions example/browser/browser-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
(function () {
const script = document.createElement("script");
script.innerHTML = `
import * as LINEJS from "https://esm.sh/v135/@jsr/[email protected]/es2022/evex__linejs.mjs";
document.body.innerHTML = "";
class LocalStorage {
prefix = "linejs:";
set(
key,
value,
) {
localStorage.setItem(this.prefix + key, JSON.stringify(value));
}
get(
key,
) {
try {
return JSON.parse(
localStorage.getItem(this.prefix + key) || "null",
);
} catch (_) {
}
}
delete(key) {
localStorage.removeItem(this.prefix + key);
}
clear() {
localStorage.clear();
}
migrate(storage) {
for (let index = 0; index < localStorage.length; index++) {
const k = localStorage.key(index);
if (k) {
storage.set(
k.replace(this.prefix, ""),
localStorage.getItem(k),
);
} else {
continue;
}
}
}
}
function log(message) {
const p = document.createElement("p");
p.innerText = message;
document.body.appendChild(p);
}
globalThis.LINEJS = LINEJS;
const client = new LINEJS.Client({ device: prompt("device?", "IOSIPAD"), storage: new LocalStorage(), endpoint: location.hostname });
client.fetch = window.fetch.bind(window);
client.on("log", (p) => console.log(p));
client.on("qrcall", (q) => {
const div = document.createElement("div");
div.innerText = "read qrcode: ";
const img = document.createElement("img");
img.src = \`https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=\${encodeURIComponent(q)}\`;
div.appendChild(img);
document.body.appendChild(div);
});
client.on("pincall", (pincode) => {
log(\`enter pincode: \${pincode}\`);
});
client.on("update:authtoken", (authToken) => {
localStorage.setItem("lastAuthToken", authToken);
});
client.on("ready", async (user) => {
log(\`Logged is as \${user.displayName} (\${user.mid})\`);
console.log(client);
});
const authToken = prompt("authToken", localStorage.getItem("lastAuthToken") || "");
if (authToken) {
client.login({ authToken });
} else {
const email = prompt("email", localStorage.getItem("lastEmail") || "");
if (email) {
localStorage.setItem("lastEmail", email);
const password = prompt("password", localStorage.getItem("lastPw") || "");
localStorage.setItem("lastPw", password);
client.login({ password, email });
} else {
client.login();
}
}
globalThis.client = client;
`;
script.type = "module";
document.body.appendChild(script);
})()
33 changes: 33 additions & 0 deletions example/square/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Client } from "@evex/linejs";
import { FileStorage } from "@evex/linejs/storage";

const client = new Client({
device: "IOSIPAD",
storage: new FileStorage("storage.json"),
});

client.on("log", (d) => {
try {
// console.log(d.type, d.data); // for debug
} catch {}
});
client.on("square:message", async (message) => {
console.log(message.text);
if (message.text === "!ping") {
await message.react("NICE");
await message.reply("pong!");
}
});
client.on("pincall", (p) => console.log("enter pincode:", p));
client.on("qrcall", (q) => console.log("qrcode:", q));
client.on("update:authtoken", (a) => console.log("AuthToken:", a));
client.on("ready", (user) => {
console.log(`Logged in as ${user.displayName} (${user.mid})`);
});

await client.login({
email: "email",
password: "pass",
});

client.polling(["square"]); // polling square
32 changes: 32 additions & 0 deletions example/square/square-chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Client } from "@evex/linejs";
import { FileStorage } from "@evex/linejs/storage";

const client = new Client({
device: "IOSIPAD",
storage: new FileStorage("storage.json"),
});

client.on("log", (d) => {
try {
// console.log(d.type, d.data); // for debug
} catch {}
});
client.on("pincall", (p) => console.log("enter pincode:", p));
client.on("qrcall", (q) => console.log("qrcode:", q));
client.on("update:authtoken", (a) => console.log("AuthToken:", a));
client.on("ready", (user) => {
console.log(`Logged in as ${user.displayName} (${user.mid})`);
});

await client.login({
email: "email",
password: "pass",
});

const chat = await client.getSquareChat("m..."); // your squareChatMid

chat.on("message", async (message) => {
await message.react("NICE");
});

chat.polling();
33 changes: 33 additions & 0 deletions example/talk/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Client } from "@evex/linejs";
import { FileStorage } from "@evex/linejs/storage";

const client = new Client({
device: "IOSIPAD",
storage: new FileStorage("storage.json"),
});

client.on("log", (d) => {
try {
// console.log(d.type, d.data); // for debug
} catch {}
});
client.on("message", async (message) => {
console.log(message.text);
if (message.text === "!ping") {
await message.react("NICE");
await message.reply("pong!");
}
});
client.on("pincall", (p) => console.log("enter pincode:", p));
client.on("qrcall", (q) => console.log("qrcode:", q));
client.on("update:authtoken", (a) => console.log("AuthToken:", a));
client.on("ready", (user) => {
console.log(`Logged in as ${user.displayName} (${user.mid})`);
});

await client.login({
email: "email",
password: "pass",
});

client.polling(["talk"]); // polling talk
Loading

0 comments on commit 7235515

Please sign in to comment.