utilizzando pyvoip e facendo il prort forwarding sul modem locare, effettuare chiamate…
- User
utilizzando pyvoip e facendo il prort forwarding sul modem locare, effettuare chiamate voip, mi gereri lo script?
Examples Here we will go over a few basic phone setups.
Setup PyVoIP uses callback functions to initiate phone calls. In the example below, our callback function is named answer. The callback takes one argument, which is a VoIPCall instance.
We are also importing VoIPPhone and InvalidStateError. VoIPPhone is the main class for our softphone. An InvalidStateError is thrown when you try to perform an impossible command. For example, denying the call when the phone is already answered, answering when it’s already answered, etc.
The following will create a phone that answers and automatically hangs up:
from pyVoIP.VoIP import VoIPPhone, InvalidStateError
def answer(call): try: call.answer() call.hangup() except InvalidStateError: pass
if name == "main": phone = VoIPPhone(, , , , myIP=<Your computer's local IP>, callCallback=answer) phone.start() input('Press enter to disable the phone') phone.stop() Announcement Board Let’s say you want to make a phone that when you call it, it plays an announcement message, then hangs up. We can accomplish this with the builtin libraries wave, audioop, time, and by importing CallState.
from pyVoIP.VoIP import VoIPPhone, InvalidStateError, CallState import time import wave
def answer(call): try: f = wave.open('announcment.wav', 'rb') frames = f.getnframes() data = f.readframes(frames) f.close()
codecall.answer() call.write_audio(data) # This writes the audio data to the transmit buffer, this must be bytes. stop = time.time() + (frames / 8000) # frames/8000 is the length of the audio in seconds. 8000 is the hertz of PCMU. while time.time() <= stop and call.state == CallState.ANSWERED: time.sleep(0.1) call.hangup() except InvalidStateError: pass except: call.hangup()if name == "main": phone = VoIPPhone(, , , , myIP=, callCallback=answer) phone.start() input('Press enter to disable the phone') phone.stop() Something important to note is our wait function. We are currently using:
stop = time.time() + (frames / 8000) # The number of frames/8000 is the length of the audio in seconds.
while time.time() <= stop and call.state == CallState.ANSWERED: time.sleep(0.1) This could be replaced with time.sleep(frames / 8000). However, doing so will not cause the thread to automatically close if the user hangs up, or if VoIPPhone().stop() is called; using the while loop method will fix this issue. The time.sleep(0.1) inside the while loop is also important. Supplementing time.sleep(0.1) for pass will cause your CPU to ramp up while running the loop, making the RTP (audio being sent out and received) lag. This can make the voice audibly slow or choppy.
- Assistant