-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
259 lines (233 loc) · 10.2 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Use the Dotenv class
use Dotenv\Dotenv;
// Load .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$apiKey = $_ENV['WEATHER_API_KEY'];
$location = $_POST['location'];
$apiUrl = "https://api.weatherapi.com/v1/current.json?key=$apiKey&q=$location&aqi=no";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
$response = curl_exec($ch);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather Report</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="weather-card">
<h1 class="title">Weather Report</h1>
<form action="" method="POST" class="weather-form">
<input type="text" placeholder="Enter your City..." name="location" class="text-input" required>
<input type="email" placeholder="Enter your Email ID" name="email" class="text-input" required>
<button type="submit" class="submit-btn">Get Weather</button>
</form>
</div>
<div>
<?php
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
$weatherData = json_decode($response, true);
// Display the weather information
if ($weatherData && isset($weatherData['current'])) {
echo '<div class="weather-box">';
echo "<h1>Current Weather in " . htmlspecialchars($location) . ":</h1><br><br>";
echo "<p>Temperature: " . $weatherData['current']["temp_c"] . "°C</p>";
echo "<p>Humidity: " . $weatherData['current']["humidity"] . "</p>";
echo "<p>Cloud: " . $weatherData['current']["cloud"] . "</p>";
echo "</div>";
}
}
// Close the cURL session
curl_close($ch);
//##### Groq AI ########
$apiUrl = "https://api.groq.com/openai/v1/chat/completions";
$apiKey = $_ENV['GROQ_API_KEY'];
$weatherCurrent = $weatherData['current'];
$email = $_POST['email'];
// Prepare the data to be sent in the request
$data = [
"messages" => [
[
"role" => "user",
"content" => "Write an email paragraph about the weather without any introductory text, headings, or concluding phrases. Start directly with 'Dear Sir/Madam,' and include only the weather data of $location, specifying the temperature as " . $weatherCurrent['temp_c'] . "°C, humidity as " . $weatherCurrent['humidity'] . "%, and cloud cover as " . $weatherCurrent['cloud'] . "%. Format the email in HTML with inline CSS within a <p> tag. Sign off with 'Best regards, Aamir.'"
]
],
"model" => "llama3-8b-8192"
];
// Initialize cURL
$ch = curl_init($apiUrl);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $apiKey",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
$responseData = json_decode($response, true);
// echo json_encode($responseData['choices'][0]['message']['content']);
if (isset($responseData['choices'][0]['message']['content'])) {
$content = $responseData['choices'][0]['message']['content'];
$mail = new PHPMailer(true);
//HTML code for sending in Email
$htmlContent = '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
color: #333;
}
.container {
width: 100%;
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
border: 1px solid #dddddd;
border-radius: 8px;
overflow: hidden;
}
.header {
background-color: #4a90e2;
color: white;
padding: 20px;
text-align: center;
}
.content {
padding: 20px;
}
.footer {
background-color: #f4f4f4;
color: #777;
text-align: center;
padding: 10px;
font-size: 12px;
}
.report-section {
margin: 20px 0;
}
.report-section h2 {
margin-bottom: 10px;
color: #4a90e2;
}
.report-item {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
}
.report-item div {
font-weight: bold;
}
.icon {
width: 50px;
height: 50px;
}
</style>
</head>
<body>
<div class="container">
<!-- Header Section -->
<div class="header">
<h1>Todays Weather Report</h1>
<p>Stay prepared with the latest weather update</p>
</div>
<!-- Content Section -->
<div class="content">
<div class="report-section">
<h2>Location: '.$location.'</h2>
<div class="report-item">
<div>Condition:</div>
<div>
'.$weatherCurrent['condition']['text'].'
</div>
</div>
<div class="report-item">
<div>Temperature:</div>
<div>'.$weatherCurrent['temp_c'].'°</div>
</div>
<div class="report-item">
<div>Cloud:</div>
<div>'.$weatherCurrent['cloud'].'%</div>
</div>
<div class="report-item">
<div>Humidity:</div>
<div>'.$weatherCurrent['humidity'].'%</div>
</div>
<div class="report-item">
<div>Wind Speed:</div>
<div>'.$weatherCurrent['wind_kph'].'</div>
</div>
</div>
</div>
<!-- Footer Section -->
<div class="footer">
<p>Weather data provided by WeatherAPI. This is an automated message; please do not reply.</p>
<p>© 2024 Weather Report Service</p>
</div>
</div>
<br> <br>
</body>
</html>
';
try {
// SMTP server settings for sending mail
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = $_ENV['MAIL_USERNAME'];
$mail->Password = $_ENV['MAIL_PASSWORD'];
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Email settings
$mail->setFrom($_ENV['MAIL_USERNAME'], 'Aamir');
$mail->addAddress($email, 'Recipient Aamir');
$mail->isHTML(true);
$mail->Subject = 'Weather Report';
$mail->Body = $htmlContent . $responseData['choices'][0]['message']['content'];
$mail->AltBody = 'This is a weather report';
$mail->send();
echo '<h3>Email has been sent</h3>';
} catch (Exception $e) {
if($mail->ErrorInfo == "Invalid address: (to):"){
echo 'Please insert Email';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
} else {
echo 'Content not found in the response.';
}
}
// Close cURL
curl_close($ch);
?>
</div>
</div>
</body>
</html>