Help with Apple Script Code to Send SMS & iMessage from Terminal

Hello! I'm looking for a snippet of code that can allow me to send iMessages from the terminal (or an SMS in the event that the message recipient does not have iMessages). I have code that can do one, or the other. But, I want some code that can do both.

PLEASE HELP!

Here is what I have right now

This code sends a text message from the command line


on run {targetBuddyPhone, targetMessage}
  tell application "Messages"
    set targetService to 1st service whose service type = SMS 
    set targetBuddy to buddy targetBuddyPhone of targetService
    send targetMessage to targetBuddy
  end tell
end run 

And this code sends an iMessage from the command line


on run {targetBuddyPhone, targetMessage}
  tell application "Messages"
    set targetService to 1st service whose service type = iMessage
    set targetBuddy to buddy targetBuddyPhone of targetService
    send targetMessage to targetBuddy
  end tell
end run 


I’m looking for a solution to combine this code into a single snippet that will try to send an iMessage if the recipient has iMessages, but if they do not, then the code will send a SMS message. 
Help with Apple Script Code to Send SMS & iMessage from Terminal
 
 
Q