fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <curl/curl.h>
  5.  
  6. // Twilio credentials
  7. #define ACCOUNT_SID "YOUR_TWILIO_ACCOUNT_SID"
  8. #define AUTH_TOKEN "YOUR_TWILIO_AUTH_TOKEN"
  9. #define TWILIO_PHONE_NUMBER "+YOUR_TWILIO_PHONE_NUMBER"
  10.  
  11. // Function to calculate electricity bill based on slabs
  12. float calculateElectricityBill(int units) {
  13. float rate;
  14. if (units <= 100) {
  15. rate = 3.00;
  16. } else if (units <= 200) {
  17. rate = 4.50;
  18. } else if (units <= 300) {
  19. rate = 6.50;
  20. } else {
  21. rate = 8.00;
  22. }
  23. return units * rate;
  24. }
  25.  
  26. // Function to calculate phone bill based on call minutes
  27. float calculatePhoneBill(int callMinutes) {
  28. float rate = 1.00; // ₹1 per minute
  29. return callMinutes * rate;
  30. }
  31.  
  32. // URL Encoding function for safe transmission
  33. char* url_encode(const char* str) {
  34. CURL *curl = curl_easy_init();
  35. if (curl) {
  36. char *output = curl_easy_escape(curl, str, 0);
  37. curl_easy_cleanup(curl);
  38. return output;
  39. }
  40. return NULL;
  41. }
  42.  
  43. // Function to send SMS using Twilio API
  44. void sendSMS(const char *recipientPhoneNumber, const char *customerName, float electricityBill, float phoneBill) {
  45. CURL *curl;
  46. CURLcode res;
  47.  
  48. // Construct the SMS message
  49. char message[512];
  50. snprintf(message, sizeof(message),
  51. "Dear %s, your electricity bill is ₹%.2f and your phone bill is ₹%.2f. Total amount due: ₹%.2f. Thank you!",
  52. customerName, electricityBill, phoneBill, electricityBill + phoneBill);
  53.  
  54. // URL encode the message
  55. char *encodedMessage = url_encode(message);
  56.  
  57. // Construct the URL dynamically
  58. char url[256];
  59. snprintf(url, sizeof(url),
  60. "https://a...content-available-to-author-only...o.com/2010-04-01/Accounts/%s/Messages.json",
  61. ACCOUNT_SID);
  62.  
  63. // Prepare POST fields
  64. char postFields[1024];
  65. snprintf(postFields, sizeof(postFields),
  66. "To=%s&From=%s&Body=%s",
  67. recipientPhoneNumber, TWILIO_PHONE_NUMBER, encodedMessage);
  68.  
  69. // Initialize cURL
  70. curl_global_init(CURL_GLOBAL_ALL);
  71. curl = curl_easy_init();
  72.  
  73. if (curl) {
  74. curl_easy_setopt(curl, CURLOPT_URL, url);
  75. curl_easy_setopt(curl, CURLOPT_USERNAME, ACCOUNT_SID);
  76. curl_easy_setopt(curl, CURLOPT_PASSWORD, AUTH_TOKEN);
  77. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields);
  78.  
  79. // Disable SSL verification (TEMPORARY FIX)
  80. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  81. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  82.  
  83. // Perform the request, and check for errors
  84. res = curl_easy_perform(curl);
  85. if (res != CURLE_OK) {
  86. fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  87. } else {
  88. printf("\nSMS sent successfully to %s!\n", recipientPhoneNumber);
  89. }
  90.  
  91. // Cleanup
  92. curl_easy_cleanup(curl);
  93. }
  94.  
  95. curl_global_cleanup();
  96. if (encodedMessage) {
  97. curl_free(encodedMessage);
  98. }
  99. }
  100.  
  101. int main() {
  102. char customerName[50];
  103. char recipientPhoneNumber[15];
  104. int electricityUnits, phoneMinutes;
  105. float electricityBill, phoneBill, totalBill;
  106.  
  107. // Input customer name
  108. printf("Enter customer name: ");
  109. fgets(customerName, sizeof(customerName), stdin);
  110. customerName[strcspn(customerName, "\n")] = '\0'; // Remove newline character
  111.  
  112. // Input customer phone number
  113. printf("Enter recipient phone number (in E.164 format, e.g., +919876543210): ");
  114. fgets(recipientPhoneNumber, sizeof(recipientPhoneNumber), stdin);
  115. recipientPhoneNumber[strcspn(recipientPhoneNumber, "\n")] = '\0'; // Remove newline character
  116.  
  117. // Input electricity usage
  118. printf("Enter electricity units consumed: ");
  119. scanf("%d", &electricityUnits);
  120.  
  121. // Input phone usage
  122. printf("Enter phone call minutes used: ");
  123. scanf("%d", &phoneMinutes);
  124.  
  125. // Calculate bills
  126. electricityBill = calculateElectricityBill(electricityUnits);
  127. phoneBill = calculatePhoneBill(phoneMinutes);
  128. totalBill = electricityBill + phoneBill;
  129.  
  130. // Display bill summary
  131. printf("\n--- Bill Summary ---\n");
  132. printf("Customer Name: %s\n", customerName);
  133. printf("Electricity Bill: ₹%.2f\n", electricityBill);
  134. printf("Phone Bill: ₹%.2f\n", phoneBill);
  135. printf("Total Bill Amount: ₹%.2f\n", totalBill);
  136.  
  137. // Send SMS notification
  138. sendSMS(recipientPhoneNumber, customerName, electricityBill, phoneBill);
  139.  
  140. return 0;
  141. }
  142.  
Success #stdin #stdout 0.02s 25976KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

// Twilio credentials
#define ACCOUNT_SID "YOUR_TWILIO_ACCOUNT_SID"
#define AUTH_TOKEN "YOUR_TWILIO_AUTH_TOKEN"
#define TWILIO_PHONE_NUMBER "+YOUR_TWILIO_PHONE_NUMBER"

// Function to calculate electricity bill based on slabs
float calculateElectricityBill(int units) {
    float rate;
    if (units <= 100) {
        rate = 3.00;
    } else if (units <= 200) {
        rate = 4.50;
    } else if (units <= 300) {
        rate = 6.50;
    } else {
        rate = 8.00;
    }
    return units * rate;
}

// Function to calculate phone bill based on call minutes
float calculatePhoneBill(int callMinutes) {
    float rate = 1.00;  // ₹1 per minute
    return callMinutes * rate;
}

// URL Encoding function for safe transmission
char* url_encode(const char* str) {
    CURL *curl = curl_easy_init();
    if (curl) {
        char *output = curl_easy_escape(curl, str, 0);
        curl_easy_cleanup(curl);
        return output;
    }
    return NULL;
}

// Function to send SMS using Twilio API
void sendSMS(const char *recipientPhoneNumber, const char *customerName, float electricityBill, float phoneBill) {
    CURL *curl;
    CURLcode res;

    // Construct the SMS message
    char message[512];
    snprintf(message, sizeof(message),
             "Dear %s, your electricity bill is ₹%.2f and your phone bill is ₹%.2f. Total amount due: ₹%.2f. Thank you!",
             customerName, electricityBill, phoneBill, electricityBill + phoneBill);

    // URL encode the message
    char *encodedMessage = url_encode(message);

    // Construct the URL dynamically
    char url[256];
    snprintf(url, sizeof(url),
             "https://a...content-available-to-author-only...o.com/2010-04-01/Accounts/%s/Messages.json",
             ACCOUNT_SID);

    // Prepare POST fields
    char postFields[1024];
    snprintf(postFields, sizeof(postFields),
             "To=%s&From=%s&Body=%s",
             recipientPhoneNumber, TWILIO_PHONE_NUMBER, encodedMessage);

    // Initialize cURL
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_USERNAME, ACCOUNT_SID);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, AUTH_TOKEN);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields);

        // Disable SSL verification (TEMPORARY FIX)
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

        // Perform the request, and check for errors
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        } else {
            printf("\nSMS sent successfully to %s!\n", recipientPhoneNumber);
        }

        // Cleanup
        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    if (encodedMessage) {
        curl_free(encodedMessage);
    }
}

int main() {
    char customerName[50];
    char recipientPhoneNumber[15];
    int electricityUnits, phoneMinutes;
    float electricityBill, phoneBill, totalBill;

    // Input customer name
    printf("Enter customer name: ");
    fgets(customerName, sizeof(customerName), stdin);
    customerName[strcspn(customerName, "\n")] = '\0';  // Remove newline character

    // Input customer phone number
    printf("Enter recipient phone number (in E.164 format, e.g., +919876543210): ");
    fgets(recipientPhoneNumber, sizeof(recipientPhoneNumber), stdin);
    recipientPhoneNumber[strcspn(recipientPhoneNumber, "\n")] = '\0';  // Remove newline character

    // Input electricity usage
    printf("Enter electricity units consumed: ");
    scanf("%d", &electricityUnits);

    // Input phone usage
    printf("Enter phone call minutes used: ");
    scanf("%d", &phoneMinutes);

    // Calculate bills
    electricityBill = calculateElectricityBill(electricityUnits);
    phoneBill = calculatePhoneBill(phoneMinutes);
    totalBill = electricityBill + phoneBill;

    // Display bill summary
    printf("\n--- Bill Summary ---\n");
    printf("Customer Name: %s\n", customerName);
    printf("Electricity Bill: ₹%.2f\n", electricityBill);
    printf("Phone Bill: ₹%.2f\n", phoneBill);
    printf("Total Bill Amount: ₹%.2f\n", totalBill);

    // Send SMS notification
    sendSMS(recipientPhoneNumber, customerName, electricityBill, phoneBill);

    return 0;
}