I don't understand how the code you posted above is relating to a RingCentral API call as I don't see a RingCentral endpoint in the code at all.
The answer to your question is "No", there is no event notification for an IVR keypress so it is not possible to detect what a caller press to trigger your code to send a text message to the caller.
However, you can implement a workaround solution like this:
Set your IVR keypress options as normal and use one of the option for sending a text message to the caller e.g. press 4 for receiving a text message. Then at the IVR key press setting, set the key press 4 to route the call to a virtual extension. This will route the incoming call to that virtual extension when the caller press 4.
In your app, you need to subscribe for the extension telephony session event notification and you will receive incoming call notification events when a call is routed to that virtual extension. As soon as you get the incoming call notification, you can grab the caller's phone number and drop the incoming call using this API. Then call the /sms API to send a text message to the caller. If you think that this workaround solution is acceptable, let me know then I can consult you more on the sending SMS messages part.
Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.
Download RingCentral Phone for Desktop:
Tip: switch to the "sandbox mode" before logging in the app:
9 People are following this question.