• Resolved nickkelley177

    (@nickkelley177)


    Before I write a custom plugin or modify the texty plugin Thought i would reach out and see if this is a feature y’all may want to intergrate. Twilio has a lookup API to verify if the number is a landline or mobile number, it would be cheaper to pay the $0.005 per lookup than to send an undeliverable message.
    I linked to the post from twilio explaining it, also here is the code I am about to test to add verification to the phone number but should work directly in your plugin as well. I look forward to your reply, thank you!

    <?php
    /**
    * twilio_lookup.php
    */

    require 'vendor/autoload.php';
    use Twilio\Rest\Client;

    function is_mobile_number($phone_number) {
    $sid = 'YOUR_TWILIO_ACCOUNT_SID';
    $token = 'YOUR_TWILIO_AUTH_TOKEN';
    $client = new Client($sid, $token);

    try {
    $number = $client->lookups->v1->phoneNumbers($phone_number)->fetch(["type" => "carrier"]);
    return $number->carrier['type'] === 'mobile';
    } catch (Exception $e) {
    error_log("Error: " . $e->getMessage());
    return false;
    }
    }

    // Example usage
    $phone_number = '+1234567890';
    if (is_mobile_number($phone_number)) {
    echo "It's a mobile number.";
    } else {
    echo "It's not a mobile number.";
    }
    <?php
    /**
    * twilio-texty-integration.php
    * Plugin Name: Twilio Texty Integration
    * Description: Integrates Twilio Lookup API with Texty plugin to filter landlines.
    * Version: 1.0
    * Author: Your Name
    */

    function filter_landline_numbers($phone_number) {
    require_once 'path/to/twilio_lookup.php';
    return is_mobile_number($phone_number);
    }

    function custom_send_sms($message, $phone_number) {
    if (filter_landline_numbers($phone_number)) {
    // Call the original Texty send function
    texty_send_sms($message, $phone_number);
    } else {
    error_log("The number $phone_number is not a mobile number.");
    }
    }

    add_action('texty_send_sms', 'custom_send_sms', 10, 2);

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter nickkelley177

    (@nickkelley177)

    Hey y’all! I added number verification to your plugin and its working as I had hoped, I will share the modified code below.


    *note: since i only use twilio I did not add any verification that twilio is the gateway in use before validation, I would imagine that’s not how you want the feature to work if it is integrated but that wouldn’t be hard to fix if your company does decide to adopt this feature*

    ~/Api/send.php

    <?php

    namespace Texty\Api;

    use WP_REST_Server;
    use Twilio\Rest\Client;

    class Send extends Base {

    /**
    * Initialize
    *
    * @return void
    */
    public function __construct() {
    $this->namespace = 'texty/v1';
    $this->rest_base = 'send';
    }

    /**
    * Registers the routes for the objects of the controller.
    *
    * @return void
    */
    public function register_routes() {
    register_rest_route(
    $this->namespace,
    '/' . $this->rest_base,
    [
    [
    'methods' => WP_REST_Server::CREATABLE,
    'callback' => [ $this, 'send' ],
    'permission_callback' => [ $this, 'admin_permissions_check' ],
    'args' => [
    'to' => [
    'description' => __( 'The to phone number', 'texty' ),
    'type' => 'string',
    'required' => true,
    ],
    'message' => [
    'description' => __( 'The message to be sent', 'texty' ),
    'type' => 'string',
    'required' => true,
    ],
    ],
    ],
    ]
    );
    }

    /**
    * Verify if the phone number is a mobile number
    *
    * @param string $phone_number
    *
    * @return bool
    */
    private function is_mobile_number($phone_number) {
    $sid = 'xxxxxxxxxredactedxxxxxxxxxxxx';
    $token = 'xxxxxxxxxredactedxxxxxxxxxxxx';
    $twilio = new Client($sid, $token);

    try {
    $number = $twilio->lookups->v1->phoneNumbers($phone_number)->fetch(["type" => "carrier"]);
    return $number->carrier['type'] === 'mobile';
    } catch (\Exception $e) {
    error_log("Error: " . $e->getMessage());
    return false;
    }
    }

    /**
    * Send the message
    *
    * @param WP_Rest_Request $request
    *
    * @return WP_Rest_Response|WP_Error
    */
    public function send( $request ) {
    $to = $request->get_param( 'to' );
    $message = $request->get_param( 'message' );

    if (!$this->is_mobile_number($to)) {
    return rest_ensure_response([
    'success' => false,
    'message' => 'Please use a mobile number or leave blank.',
    ]);
    }

    try {
    $status = texty()->gateways()->send( $to, $message );

    $response = [
    'success' => is_wp_error( $status ) ? false : true,
    'message' => is_wp_error( $status ) ? $status->get_error_message() : 'Message sent successfully',
    ];
    } catch (\Exception $e) {
    $response = [
    'success' => false,
    'message' => $e->getMessage(),
    ];
    }

    return rest_ensure_response($response);
    }
    }
    [video src="https://www.kcplantfactory.com/wp-content/uploads/add_from_server/Videos/Texty_2024-06-11.mp4" /]
    Successful test! it works great

    I will start working on implementing that on the actual logic that sends the eme order notifications next! Wish me luck ??

    Thread Starter nickkelley177

    (@nickkelley177)

    Following up on this, has anyone taken a look into it yet?

    – ‘kiasarecool AKA nickkelley177’
    Plugin Support Yeasin Arafat

    (@yeasinarafathridoy)

    Hi nickkelley177?,

    Thank you for your message. I understand that you’re looking to integrate Twilio’s lookup API for verifying phone number types. We request that, please submit a pull request (PR) for the Texty plugin using the URL below https://github.com/weDevsOfficial/texty. Our developer will review the PR and continue with the implementation. This will allow us to merge the feature directly into the Texty plugin. I hope this clarifies things.

    All the best,

    Plugin Support Yeasin Arafat

    (@yeasinarafathridoy)

    Hi nickkelley177,
    As we haven’t heard back from you for a while, we’ll consider this topic resolved. If you encounter any further issues, please don’t hesitate to open a new topic.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.