fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <curl/curl.h>
  6.  
  7. // Function to generate a 6-digit OTP
  8. int generateOTP() {
  9. return (rand() % 900000) + 100000;
  10. }
  11.  
  12. // Function to send the OTP via Twilio API
  13. void sendOTP(const char* otp, const char* to_number, const char* from_number, const char* account_sid, const char* auth_token) {
  14. CURL *curl;
  15. CURLcode res;
  16.  
  17. // Initialize CURL
  18. curl_global_init(CURL_GLOBAL_DEFAULT);
  19. curl = curl_easy_init();
  20.  
  21. if(curl) {
  22. char url[256];
  23. sprintf(url, "https://a...content-available-to-author-only...o.com/2010-04-01/Accounts/%s/Messages.json", account_sid);
  24.  
  25. // Message body
  26. char data[512];
  27. sprintf(data, "To=%s&From=%s&Body=Your+OTP+is+%s", to_number, from_number, otp);
  28.  
  29. // Set up the request
  30. curl_easy_setopt(curl, CURLOPT_URL, url);
  31. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  32.  
  33. // Set authentication
  34. char auth[128];
  35. sprintf(auth, "%s:%s", account_sid, auth_token);
  36. curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
  37.  
  38. // Perform the request
  39. res = curl_easy_perform(curl);
  40.  
  41. // Check for errors
  42. if(res != CURLE_OK)
  43. fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  44. else
  45. printf("OTP sent successfully!\n");
  46.  
  47. // Cleanup
  48. curl_easy_cleanup(curl);
  49. }
  50. curl_global_cleanup();
  51. }
  52.  
  53. int main() {
  54. // Seed the random number generator
  55. srand(time(NULL));
  56.  
  57. // Generate the OTP
  58. int otp = generateOTP();
  59. char otp_str[7];
  60. sprintf(otp_str, "%d", otp);
  61.  
  62. // Twilio credentials and numbers
  63. const char* account_sid = "your_account_sid";
  64. const char* auth_token = "your_auth_token";
  65. const char* to_number = "+9145815895"; // Replace with the recipient's number
  66. const char* from_number = "+9718606781"; // Replace with your Twilio number
  67.  
  68. // Send the OTP
  69. sendOTP(otp_str, to_number, from_number, account_sid, auth_token);
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0.03s 25804KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <curl/curl.h>

// Function to generate a 6-digit OTP
int generateOTP() {
    return (rand() % 900000) + 100000;
}

// Function to send the OTP via Twilio API
void sendOTP(const char* otp, const char* to_number, const char* from_number, const char* account_sid, const char* auth_token) {
    CURL *curl;
    CURLcode res;

    // Initialize CURL
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if(curl) {
        char url[256];
        sprintf(url, "https://a...content-available-to-author-only...o.com/2010-04-01/Accounts/%s/Messages.json", account_sid);

        // Message body
        char data[512];
        sprintf(data, "To=%s&From=%s&Body=Your+OTP+is+%s", to_number, from_number, otp);

        // Set up the request
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

        // Set authentication
        char auth[128];
        sprintf(auth, "%s:%s", account_sid, auth_token);
        curl_easy_setopt(curl, CURLOPT_USERPWD, auth);

        // Perform the request
        res = curl_easy_perform(curl);

        // Check for errors
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        else
            printf("OTP sent successfully!\n");

        // Cleanup
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
}

int main() {
    // Seed the random number generator
    srand(time(NULL));

    // Generate the OTP
    int otp = generateOTP();
    char otp_str[7];
    sprintf(otp_str, "%d", otp);

    // Twilio credentials and numbers
    const char* account_sid = "your_account_sid";
    const char* auth_token = "your_auth_token";
    const char* to_number = "+9145815895";     // Replace with the recipient's number
    const char* from_number = "+9718606781";   // Replace with your Twilio number

    // Send the OTP
    sendOTP(otp_str, to_number, from_number, account_sid, auth_token);

    return 0;
}