Linework Developers
HI
एक्सेस पाएँ

गाइड · chat:write

कस्टमर-सपोर्ट चैट bot

आपके अकाउंट को लिखने वाले लोगों को जवाब दें: ऑर्डर से जुड़े सवाल, खुलने का समय, FAQs। ज़रूरत पड़ने पर बातचीत किसी इंसान को सौंप दें।

Scopes: chat:read, chat:write (वैकल्पिक: ऑर्डर देखने के लिए orders:read)।

Anti-spam नियम#

एक bot सिर्फ़ वहीं लिख सकता है जहाँ दूसरे व्यक्ति ने पहले ही लिखा है, या उन लोगों को जो अकाउंट को फ़ॉलो करते हैं। बाकी सब कुछ 403 CHAT_NOT_ALLOWED के साथ fail होता है। ब्लॉक हमेशा लागू होते हैं (403 CHAT_BLOCKED)। इससे सपोर्ट bots बनाना आसान और बिना माँगे संपर्क (cold outreach) करना असंभव हो जाता है — जानबूझकर।

Endpoints#

Endpointइस्तेमाल
GET /conversationsआपकी बातचीत, सबसे हाल की गतिविधि वाली पहले (paginated)।
GET /conversations/{id}/messagesकिसी बातचीत के मैसेज, सबसे नए पहले (paginated)।
POST /conversations/{id}/messagesकिसी मौजूदा बातचीत में जवाब दें।
POST /messagesकिसी यूज़र को उसकी id से लिखें (ज़रूरत हो तो बातचीत खोलता है)। Anti-spam नियम के अधीन।

1. जवाब देने लायक बातचीत ढूँढें#

curl "https://api.linework.app/open/v1/conversations?limit=20" \
  -H "Authorization: Bearer $LINEWORK_API_KEY"

नई गतिविधि वाली हर बातचीत के लिए, हाल के मैसेज पढ़ें और देखें कि आख़िरी बार किसने लिखा: अगर वह आप नहीं हैं, तो शायद bot के पास जवाब देने के लिए कुछ हो।

curl "https://api.linework.app/open/v1/conversations/c_3391/messages?limit=10" \
  -H "Authorization: Bearer $LINEWORK_API_KEY"

2. जवाब दें#

curl https://api.linework.app/open/v1/conversations/c_3391/messages \
  -H "Authorization: Bearer $LINEWORK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "text": "Hi! We are open Monday to Saturday, 9:00–19:00." }'

3. एक पूरा polling bot#

const API = "https://api.linework.app/open/v1";
const headers = { Authorization: `Bearer ${process.env.LINEWORK_API_KEY}`, "Content-Type": "application/json" };
const get = (p) => fetch(API + p, { headers }).then((r) => r.json());

const me = await get("/me");
const answered = new Set(); // in production: a database

async function tick() {
  const convs = await get("/conversations?limit=20");
  for (const conv of convs.data) {
    const msgs = await get(`/conversations/${conv.id}/messages?limit=5`);
    const last = msgs.data[0]; // newest first
    if (!last || last.senderId === me.id || answered.has(last.id)) continue;

    const reply = answer(last.text);
    answered.add(last.id);
    if (!reply) continue; // a human will answer

    const res = await fetch(`${API}/conversations/${conv.id}/messages`, {
      method: "POST", headers, body: JSON.stringify({ text: reply }),
    });
    if (res.status === 429) return; // quota or rate limit: try again later
  }
}

function answer(text = "") {
  if (/hours|open|orari/i.test(text)) return "We are open Monday to Saturday, 9:00–19:00.";
  if (/order|ordine/i.test(text)) return "Send me your order number and I will check it for you.";
  return null;
}

setInterval(() => tick().catch(console.error), 30_000);
import os, re, time, requests

API = "https://api.linework.app/open/v1"
s = requests.Session()
s.headers["Authorization"] = f"Bearer {os.environ['LINEWORK_API_KEY']}"

me = s.get(API + "/me", timeout=30).json()
answered = set()  # in production: a database

def answer(text):
    if re.search(r"hours|open|orari", text or "", re.I):
        return "We are open Monday to Saturday, 9:00–19:00."
    if re.search(r"order|ordine", text or "", re.I):
        return "Send me your order number and I will check it for you."
    return None

def tick():
    convs = s.get(API + "/conversations", params={"limit": 20}, timeout=30).json()
    for conv in convs["data"]:
        msgs = s.get(f"{API}/conversations/{conv['id']}/messages", params={"limit": 5}, timeout=30).json()
        last = msgs["data"][0] if msgs["data"] else None  # newest first
        if not last or last["senderId"] == me["id"] or last["id"] in answered:
            continue
        answered.add(last["id"])
        reply = answer(last.get("text"))
        if not reply:
            continue  # a human will answer
        r = s.post(f"{API}/conversations/{conv['id']}/messages", json={"text": reply}, timeout=30)
        if r.status_code == 429:
            return  # quota or rate limit: try again later

while True:
    tick()
    time.sleep(30)

senderId जैसे फ़ील्ड नाम सिर्फ़ उदाहरण के लिए दिखाए गए हैं: मैसेज का schema API Reference में जाँचें।

शालीनता से polling करें#

  • हर 15–60 सेकंड में poll करें। हर tick में एक /conversations call और कुछ मैसेज पढ़ना, प्रति मिनट 120 requests से काफ़ी नीचे रहता है।
  • सिर्फ़ वही बातचीत खोलें जिनकी आख़िरी गतिविधि पिछले tick के बाद बदली है।
  • दैनिक quota हर अकाउंट के लिए 500 मैसेज है।

एक अच्छा bot बनें#

  • पहले जवाब में बताएँ कि यह एक bot है, और लोगों को बताएँ कि किसी इंसान तक कैसे पहुँचें।
  • जिन लोगों ने नहीं माँगा, उन्हें कभी प्रमोशन न भेजें, और एक ही मैसेज कई लोगों को कभी न भेजें।
  • चैट में पासवर्ड, भुगतान की जानकारी या codes न माँगें।
  • जब लोग रुकने को कहें, तो रुक जाएँ।