STANAANDREY

send vonage message

Nov 25th, 2025
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. from vonage import Vonage, Auth
  2. from vonage_sms import SmsMessage
  3.  
  4. def main():
  5.     # 1. Use the new Vonage and Auth classes for initialization
  6.     client = Vonage(Auth(api_key="476f27f2", api_secret="..."))
  7.  
  8.     # 2. Use the SmsMessage object instead of a dictionary
  9.     # Note: Use 'from_' because 'from' is a reserved keyword in Python
  10.     message = SmsMessage(
  11.         to="40750760880",
  12.         from_="Vonage APIs",
  13.         text="A text message sent using the Vonage SMS API"
  14.     )
  15.  
  16.     # 3. Send the message
  17.     response = client.sms.send(message)
  18.  
  19.     # 4. Access response data (it returns an object, not a dictionary now)
  20.     if response.messages[0].status == "0":
  21.         print("Message sent successfully.")
  22.     else:
  23.         print(f"Message failed with error: {response.messages[0].error_text}")
  24.  
  25. if __name__ == '__main__':
  26.     main()
Advertisement
Add Comment
Please, Sign In to add comment