We are using Asterisk and Bandwidth.com to place call to PSTN numbers. We want Asterisk to perform certain actions if call goes to voicemail of dialed number. For that we want to identify when call goes to voicemail and change dial-plan accordingly.
Can asterisk know when the call goes to voicemail of called number?
Unless the service provider sends a connected line update, and you know the number used for the voicemail redirect, no. Telephone signalling systems do not have an explicit way of saying that a call has gone to voicemail.
Asterisk has an answering machine detection mode, but that just uses heuristics based on the way that humans answer real calls as against how answering machine greetings are done. Some phone spammers may also take account of the typical 25 second timeout.