Skip to content

כיצד לטפל בשיחה נכנסת#

גרסת בטא

הפונקציונליות נמצאת בגרסת בטא. הפונקציות עשויות להשתנות ועלולות לפעול באופן לא יציב. ייתכן שבעתיד תיגבה תשלום נוסף עבור הפונקציונליות.
ניתן לבקש גישה לפונקציונליות דרך התמיכה הטכנית של GREEN-API

על שיחה נכנסת החיבור מדווח באמצעות האירוע incoming-call. הוא מתרחש פעם אחת ומכיל ב-event.detail את מזהה השיחה id, את מזהה הצד השני ב-WhatsApp ‏wid ואת שמו name, אם WhatsApp העביר את השם.

קבלת השיחה מתבצעת בשיטה accept של הלקוח, והעברת השמע מופעלת אחריה, בדיוק בסדר הזה (הסיבה — ראו העברת שמע).

const audioElement = document.createElement('audio');

calls.addEventListener('incoming-call', async (event) => {
  const { id, wid, name } = event.detail;
  console.log('Incoming call from:', name || wid, 'Call ID:', id);

  // Show your own incoming call window here;
  // in this example, the call is accepted immediately.
  try {
    await client.accept();
    await calls.startAudioBridge();
    console.log('Call accepted.');
  } catch (error) {
    console.error('Error accepting the call:', error);
  }
});

calls.addEventListener('remote-stream-ready', (event) => {
  audioElement.srcObject = event.detail;
  audioElement.play();
});

חלון השיחה הנכנסת#

בדוגמאות, עבור שיחות נכנסות נוצר חלון קופץ עם שני כפתורים — קבלה ודחייה.

const incomingCallPortal = document.getElementById('incoming-call-portal');
const incomingCallPhone = document.getElementById('incoming-call-phone');
const acceptCallButton = document.getElementById('acceptCallButton');
const rejectCallButton = document.getElementById('rejectCallButton');

calls.addEventListener('incoming-call', (event) => {
  const { wid, name } = event.detail;

  incomingCallPhone.textContent = name || wid;
  incomingCallPortal.style.display = 'flex';
});

acceptCallButton.addEventListener('click', async () => {
  incomingCallPortal.style.display = 'none';

  await client.accept();
  await calls.startAudioBridge();
});

rejectCallButton.addEventListener('click', async () => {
  incomingCallPortal.style.display = 'none';

  await client.reject();
});

יש להסתיר את החלון גם כאשר השיחה הסתיימה מעצמה — באירוע end-call: המתקשר עשוי לנתק, והשיחה עשויה להתקבל במכשיר אחר של אותו חשבון. הטיפול באירוע זה מתואר בסעיף טיפול במצבי שיחה.

הסימון (markup) והעיצוב של החלון:

    <div id="incoming-call-portal">
      <section class="incoming-call-modal">
        <section>
          Incoming call: <span id="incoming-call-phone">79836200181</span>
        </section>
        <section>
          <button class="incoming-call-reject" id="rejectCallButton">reject</button>
          <button class="incoming-call-accept" id="acceptCallButton">accept</button>
        </section>
      </section>
    </div>

וגם CSS

.incoming-call-modal {
    width: 300px;
    height: 100px;

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 20px;

    background: white;
    box-shadow: 0 0 10px #000000;
}

.incoming-call-reject {
    border: none;
    background: none;

    width: 100px;
    height: 30px;

    border-radius: 10px;

    color: #e8e8e8;
    background: #c53c3c;
}

.incoming-call-reject:hover {
    cursor: pointer;
    color: white;
    background: #e34242;
}

.incoming-call-accept {
    border: none;
    background: none;

    width: 100px;
    height: 30px;

    border-radius: 10px;

    color: #e8e8e8;
    background: #11911d;
}

.incoming-call-accept:hover {
    cursor: pointer;
    color: white;
    background: #11a41e;
}

השיטה reject דוחה שיחה נכנסת, ואילו שיחה שכבר התקבלה מסתיימת בשיטה hangUp — ראו כיצד לסיים או לדחות שיחה.