> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nonefivem.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Phone Integration

<Info>You can find phone implementations in `no-payphone/integration/phone`</Info>

Phone integration can be a little more complicated. To do this we need to add event listeners for payphone events and trigger phone events.

## Ready Integrations

***

* [qb-phone](https://github.com/qbcore-framework/qb-phone)

## Payphone Events

***

This events will be triggered by payphone.

<Accordion title="no-payphone:call:start">
  Triggered when payphone starts the call.

  ### Parameters

  * ctx: `{source: number; number: string; targetNumber: string; callId: number}`

  ctx.source is the caller's server id.

  ctx.number is payphone's number.

  ctx.targetNumber is the number payphone is calling.

  ctx.callId is the call Id generated from payphone. (You don't need to use this)
</Accordion>

<Accordion title="no-payphone:call:answer">
  Triggered when payphone answers the call.

  ### Parameters

  * ctx: `{source: number; number: string; targetNumber: string; callId: number}`

  ctx.source is the caller's server id.

  ctx.number is payphone's number.

  ctx.targetNumber is the number payphone is calling.

  ctx.callId is the call Id generated from payphone. (You don't need to use this)
</Accordion>

<Accordion title="no-payphone:call:end">
  Triggered when payphone ends the call.

  ### Parameters

  * ctx: `{number: string; targetNumber: string}`

  ctx.number is payphone's number.

  ctx.targetNumber is the number payphone is calling.
</Accordion>

## Phone Events

***

This events expected to triggered by phone.

<Accordion title="no-payphone:phone:start">
  Triggered when phone starts the call.

  ### Parameters

  * ctx: `{source: number; number: string; targetNumber: string; callId?: number}`
    ctx.source is the caller's server id.
    ctx.number is the phone's number.
    ctx.targetNumber is the number phone is calling.
    ctx.callId: Generated call id (If nil payphone will generate itself.).
</Accordion>

<Accordion title="no-payphone:phone:answer">
  Triggered when phone answers the call.

  ### Parameters

  * ctx: `{source: number; number: number}`
    ctx.source is the caller's server id.
    ctx.number is the phone's number.
</Accordion>

<Accordion title="no-payphone:phone:end">
  Triggered when phone ends the call.

  ### Parameters

  * phoneNumber: string
</Accordion>

## Example

***

<Info>
  This is an example for qb-phone. You can find this integration in
  `no-payphone/config/integration/phone/qb-phone.lua`
</Info>

```lua theme={null}
if not _IS_SERVER or not HasResource("qb-phone") then return end

-- NOTES
--- IN ORDER TO LET PLAYERS CALL NON-PLAYER NUMBERS YOU NEED TO CHANGE CALLBACK CALLED "qb-phone:server:GetCallState" IN qb-phone/server/main.lua:163 WITH THIS
-- cb(true, true) EVEN IF TARGET IS NIL
-- QBCore.Functions.CreateCallback('qb-phone:server:GetCallState', function(_, cb, ContactData)
--     local Target = QBCore.Functions.GetPlayerByPhone(ContactData.number)
--     if Target ~= nil then
--         if Calls[Target.PlayerData.citizenid] ~= nil then
--             if Calls[Target.PlayerData.citizenid].inCall then
--                 cb(false, true)
--             else
--                 cb(true, true)
--             end
--         else
--             cb(true, true)
--         end
--     else
--         cb(true, true)
--     end
-- end)

-- SERVER START

local QBCore = exports["qb-core"]:GetCoreObject()

-- payphone start the call
---@param ctx table
---@field source number
---@field number string
---@field targetNumber string
---@field callId number
AddEventHandler("no-payphone:call:start", function(ctx)
    local target = QBCore.Functions.GetPlayerByPhone(ctx.targetNumber)

    if not target then return end

    TriggerClientEvent("qb-phone:client:GetCalled", target.PlayerData.source, ctx.number, ctx.callId, false)
end)

-- payphone answer the call
---@param ctx table
---@field source number
---@field number string
---@field targetNumber string
---@field callId number
AddEventHandler("no-payphone:call:answer", function(ctx)
    local target = QBCore.Functions.GetPlayerByPhone(ctx.targetNumber)

    if not target then return end

    TriggerClientEvent("qb-phone:client:AnswerCall", target.PlayerData.source)
end)

-- payphone end the call
---@param ctx table
---@field number string
---@field targetNumber string
AddEventHandler("no-payphone:call:end", function(ctx)
    local target = QBCore.Functions.GetPlayerByPhone(ctx.targetNumber)

    if not target then return end

    TriggerClientEvent("qb-phone:client:CancelCall", target.PlayerData.source)
end)

-- phone start the call
RegisterNetEvent("qb-phone:server:CallContact", function(targetData, callId)
    local player = QBCore.Functions.GetPlayer(source)

    if not player then return end

    TriggerEvent("no-payphone:phone:start", {
        source = source, -- number
        number = player.PlayerData.charinfo.phone, -- string
        targetNumber = targetData.number, -- string
        callId = callId -- number?
    })
end)

-- phone answer the call
RegisterNetEvent("qb-phone:server:AnswerCall", function(ctx)
    local player = QBCore.Functions.GetPlayer(source)

    if not player then return end

    TriggerEvent("no-payphone:phone:answer", {
        source = source, -- number
        number = player.PlayerData.charinfo.phone
    })
end)

-- phone cancel the call
RegisterNetEvent("qb-phone:server:CancelCall", function(ctx)
    local player = QBCore.Functions.GetPlayer(source)

    if not player then return end

    TriggerEvent("no-payphone:phone:end", player.PlayerData.charinfo.phone)
end)

```
