-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
406 lines (341 loc) · 17.3 KB
/
Copy pathbot.py
File metadata and controls
406 lines (341 loc) · 17.3 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"""
bot.py - CropRadar Telegram bot (bilingual: English / Kannada)
Conversation flow:
/start
└─► Language choice (English / ಕನ್ನಡ) [WAITING_LANGUAGE]
└─► Ask for location [WAITING_LOCATION]
└─► Check nearby outbreaks
└─► Ask for crop photo [WAITING_PHOTO]
└─► Diagnose → reply → loop back for next photo
"""
from dotenv import load_dotenv
load_dotenv()
import logging
import os
import tempfile
from pathlib import Path
import requests
from telegram import (
KeyboardButton,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
Update,
)
from telegram.ext import (
Application,
CommandHandler,
ConversationHandler,
ContextTypes,
MessageHandler,
filters,
)
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
TELEGRAM_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "YOUR_BOT_TOKEN_HERE")
API_BASE_URL = os.environ.get("CROPRADAR_API_URL", "http://localhost:8000")
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# Conversation states
WAITING_LANGUAGE = 0
WAITING_LOCATION = 1
WAITING_PHOTO = 2
# Trigger texts for language buttons
LANG_EN = "🇬🇧 English"
LANG_KN = "🇮🇳 ಕನ್ನಡ"
# ---------------------------------------------------------------------------
# Bilingual string table
# ---------------------------------------------------------------------------
STRINGS = {
"en": {
"welcome": (
"🌾 *Welcome to CropRadar!*\n\n"
"I detect crop diseases from photos and alert you to outbreaks nearby.\n\n"
"Please choose your language to continue:"
),
"lang_set": "✅ Language set to *English*. Let's get started!\n\n",
"ask_location": (
"📍 *Step 1 — Share Your Location*\n\n"
"Please share your current location so I can check for disease outbreaks "
"in your area before we analyse your crop."
),
"share_btn": "📍 Share My Location",
"location_received": "✅ Location received — *{lat:.4f}°, {lon:.4f}°*\n\n🔍 Checking for disease outbreaks in your area…",
"outbreak_header": "⚠️ *Outbreak Risk Alert!*\n\nThe following diseases have been reported near you recently:\n",
"outbreak_row": "🦠 *{disease}* — {count} reports within 50 km",
"outbreak_footer": "\n\n🛡️ Apply preventive treatment and monitor your crops closely.\n\n📸 *Step 2:* Now send me a photo of your crop leaf and I'll analyse it.",
"no_outbreak": "✅ *No active outbreaks detected near you.*\n\n📸 *Step 2:* Send me a photo of your crop leaf and I'll diagnose it!",
"analysing": "🔍 Analysing your crop image, please wait…",
"diagnosis_header": "🌿 *CropRadar Diagnosis*",
"disease_label": "🦠 *Disease:*",
"confidence_label": "*Confidence:*",
"remedy_label": "💊 *Remedy:*",
"prevention_label": "🛡️ *Prevention:*",
"report_stored": "📋 _Report #{id} stored{loc}_",
"next_photo": "\n📸 Send another photo to analyse more crops, or /restart to reset.",
"conn_error": "❌ Could not connect to the CropRadar backend.\nMake sure the API is running at: {url}",
"analysis_error": "❌ Analysis failed: {err}",
"nudge_location": "⚠️ I need your *location* first — please tap the 📍 button below.",
"nudge_photo": "📸 Please send a *photo* of a crop leaf to get a diagnosis.",
"nudge_language": "Please choose a language using the buttons below:",
"session_ended": "Session ended. Type /start to begin again.",
},
"kn": {
"welcome": (
"🌾 *ಕ್ರಾಪ್ರಾಡಾರ್ಗೆ ಸ್ವಾಗತ!*\n\n"
"ನಾನು ಫೋಟೋಗಳಿಂದ ಬೆಳೆ ರೋಗಗಳನ್ನು ಪತ್ತೆ ಮಾಡುತ್ತೇನೆ ಮತ್ತು ನಿಮ್ಮ ಸುತ್ತಲಿನ "
"ರೋಗ ಹರಡುವಿಕೆ ಬಗ್ಗೆ ಎಚ್ಚರಿಸುತ್ತೇನೆ.\n\n"
"ಮುಂದುವರಿಯಲು ಭಾಷೆಯನ್ನು ಆರಿಸಿ:"
),
"lang_set": "✅ ಭಾಷೆಯನ್ನು *ಕನ್ನಡ*ಕ್ಕೆ ಹೊಂದಿಸಲಾಗಿದೆ. ಪ್ರಾರಂಭಿಸೋಣ!\n\n",
"ask_location": (
"📍 *ಹಂತ 1 — ನಿಮ್ಮ ಸ್ಥಳ ಹಂಚಿಕೊಳ್ಳಿ*\n\n"
"ನಿಮ್ಮ ಬಳಿ ರೋಗ ಹರಡುವಿಕೆ ಇದೆಯೇ ಎಂದು ಪರಿಶೀಲಿಸಲು ದಯವಿಟ್ಟು "
"ನಿಮ್ಮ ಪ್ರಸ್ತುತ ಸ್ಥಳವನ್ನು ಹಂಚಿಕೊಳ್ಳಿ."
),
"share_btn": "📍 ನನ್ನ ಸ್ಥಳ ಹಂಚಿಕೊಳ್ಳಿ",
"location_received": "✅ ಸ್ಥಳ ಸ್ವೀಕರಿಸಲಾಗಿದೆ — *{lat:.4f}°, {lon:.4f}°*\n\n🔍 ನಿಮ್ಮ ಪ್ರದೇಶದಲ್ಲಿ ರೋಗ ಹರಡುವಿಕೆ ಪರಿಶೀಲಿಸಲಾಗುತ್ತಿದೆ…",
"outbreak_header": "⚠️ *ರೋಗ ಹರಡುವಿಕೆ ಎಚ್ಚರಿಕೆ!*\n\nಇತ್ತೀಚೆಗೆ ನಿಮ್ಮ ಬಳಿ ಈ ರೋಗಗಳು ವರದಿಯಾಗಿವೆ:\n",
"outbreak_row": "🦠 *{disease}* — 50 ಕಿ.ಮೀ ಒಳಗೆ {count} ವರದಿಗಳು",
"outbreak_footer": "\n\n🛡️ ತಡೆಗಟ್ಟುವ ಚಿಕಿತ್ಸೆ ಅನ್ವಯಿಸಿ ಮತ್ತು ನಿಮ್ಮ ಬೆಳೆಗಳನ್ನು ಗಮನಿಸಿ.\n\n📸 *ಹಂತ 2:* ಈಗ ನಿಮ್ಮ ಬೆಳೆ ಎಲೆಯ ಫೋಟೋ ಕಳುಹಿಸಿ.",
"no_outbreak": "✅ *ನಿಮ್ಮ ಬಳಿ ಯಾವುದೇ ರೋಗ ಹರಡುವಿಕೆ ಕಂಡುಬಂದಿಲ್ಲ.*\n\n📸 *ಹಂತ 2:* ರೋಗ ಪತ್ತೆಗಾಗಿ ನಿಮ್ಮ ಬೆಳೆ ಎಲೆಯ ಫೋಟೋ ಕಳುಹಿಸಿ!",
"analysing": "🔍 ನಿಮ್ಮ ಬೆಳೆಯ ಚಿತ್ರ ವಿಶ್ಲೇಷಿಸಲಾಗುತ್ತಿದೆ, ದಯವಿಟ್ಟು ನಿರೀಕ್ಷಿಸಿ…",
"diagnosis_header": "🌿 *ಕ್ರಾಪ್ರಾಡಾರ್ ರೋಗ ನಿರ್ಣಯ*",
"disease_label": "🦠 *ರೋಗ:*",
"confidence_label": "*ವಿಶ್ವಾಸ:*",
"remedy_label": "💊 *ಪರಿಹಾರ:*",
"prevention_label": "🛡️ *ತಡೆಗಟ್ಟುವಿಕೆ:*",
"report_stored": "📋 _ವರದಿ #{id} ಸಂಗ್ರಹಿಸಲಾಗಿದೆ{loc}_",
"next_photo": "\n📸 ಇನ್ನೊಂದು ಫೋಟೋ ಕಳುಹಿಸಿ ಅಥವಾ /restart ಮಾಡಿ.",
"conn_error": "❌ CropRadar ಬ್ಯಾಕೆಂಡ್ಗೆ ಸಂಪರ್ಕಿಸಲು ಸಾಧ್ಯವಾಗಲಿಲ್ಲ.\n{url} ನಲ್ಲಿ API ಚಾಲನೆಯಲ್ಲಿದೆಯೇ ಎಂದು ಖಚಿತಪಡಿಸಿ.",
"analysis_error": "❌ ವಿಶ್ಲೇಷಣೆ ವಿಫಲವಾಗಿದೆ: {err}",
"nudge_location": "⚠️ ಮೊದಲು ನಿಮ್ಮ *ಸ್ಥಳ* ಬೇಕು — ದಯವಿಟ್ಟು ಕೆಳಗಿನ 📍 ಬಟನ್ ಅನ್ನು ಟ್ಯಾಪ್ ಮಾಡಿ.",
"nudge_photo": "📸 ರೋಗ ನಿರ್ಣಯ ಪಡೆಯಲು ಬೆಳೆ ಎಲೆಯ *ಫೋಟೋ* ಕಳುಹಿಸಿ.",
"nudge_language": "ದಯವಿಟ್ಟು ಕೆಳಗಿನ ಬಟನ್ಗಳನ್ನು ಬಳಸಿ ಭಾಷೆ ಆರಿಸಿ:",
"session_ended": "ಸೆಷನ್ ಮುಕ್ತಾಯವಾಗಿದೆ. ಮತ್ತೆ ಪ್ರಾರಂಭಿಸಲು /start ಟೈಪ್ ಮಾಡಿ.",
},
}
def s(context: ContextTypes.DEFAULT_TYPE, key: str) -> str:
"""Return the string for the user's chosen language (defaults to English)."""
lang = context.user_data.get("lang", "en")
return STRINGS[lang][key]
# ---------------------------------------------------------------------------
# Keyboards
# ---------------------------------------------------------------------------
def _language_keyboard() -> ReplyKeyboardMarkup:
return ReplyKeyboardMarkup(
[[KeyboardButton(LANG_EN), KeyboardButton(LANG_KN)]],
resize_keyboard=True,
one_time_keyboard=True,
)
def _location_keyboard(lang: str) -> ReplyKeyboardMarkup:
label = STRINGS[lang]["share_btn"]
return ReplyKeyboardMarkup(
[[KeyboardButton(label, request_location=True)]],
resize_keyboard=True,
one_time_keyboard=True,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _check_nearby_outbreaks(lat: float, lon: float) -> list[dict]:
try:
r = requests.get(
f"{API_BASE_URL}/nearby-alerts",
params={"lat": lat, "lon": lon, "radius_km": 50},
timeout=10,
)
r.raise_for_status()
return r.json().get("outbreaks", [])
except Exception as exc:
logger.warning("Could not fetch nearby alerts: %s", exc)
return []
# ---------------------------------------------------------------------------
# State: WAITING_LANGUAGE (entry point)
# ---------------------------------------------------------------------------
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Show language selection buttons."""
context.user_data.clear()
await update.message.reply_text(
STRINGS["en"]["welcome"], # always shown in English first
parse_mode="Markdown",
reply_markup=_language_keyboard(),
)
return WAITING_LANGUAGE
async def handle_language(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Store the user's language choice, then ask for location."""
text = update.message.text.strip()
if LANG_KN in text:
lang = "kn"
else:
lang = "en"
context.user_data["lang"] = lang
await update.message.reply_text(
s(context, "lang_set") + s(context, "ask_location"),
parse_mode="Markdown",
reply_markup=_location_keyboard(lang),
)
return WAITING_LOCATION
async def wrong_language(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Nudge user to pick a language button."""
await update.message.reply_text(
s(context, "nudge_language"),
reply_markup=_language_keyboard(),
)
return WAITING_LANGUAGE
# ---------------------------------------------------------------------------
# State: WAITING_LOCATION
# ---------------------------------------------------------------------------
async def handle_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Store location and check for nearby outbreaks."""
msg = update.message
lat = msg.location.latitude
lon = msg.location.longitude
context.user_data["lat"] = lat
context.user_data["lon"] = lon
lang = context.user_data.get("lang", "en")
await msg.reply_text(
s(context, "location_received").format(lat=lat, lon=lon),
parse_mode="Markdown",
reply_markup=ReplyKeyboardRemove(),
)
outbreaks = _check_nearby_outbreaks(lat, lon)
if outbreaks:
lines = [s(context, "outbreak_header")]
for ob in outbreaks:
lines.append(s(context, "outbreak_row").format(
disease=ob["disease_type"], count=ob["count"]
))
lines.append(s(context, "outbreak_footer"))
await msg.reply_text("\n".join(lines), parse_mode="Markdown")
else:
await msg.reply_text(s(context, "no_outbreak"), parse_mode="Markdown")
return WAITING_PHOTO
async def wrong_input_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
lang = context.user_data.get("lang", "en")
await update.message.reply_text(
s(context, "nudge_location"),
parse_mode="Markdown",
reply_markup=_location_keyboard(lang),
)
return WAITING_LOCATION
# ---------------------------------------------------------------------------
# State: WAITING_PHOTO
# ---------------------------------------------------------------------------
async def handle_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Analyse crop image and reply with diagnosis."""
msg = update.message
lat = context.user_data.get("lat")
lon = context.user_data.get("lon")
await msg.reply_text(s(context, "analysing"))
# Download photo
photo = msg.photo[-1]
tg_file = await context.bot.get_file(photo.file_id)
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
await tg_file.download_to_drive(tmp.name)
tmp_path = tmp.name
try:
with open(tmp_path, "rb") as img_file:
form_data = {"language": context.user_data.get("lang", "en")}
if lat is not None:
form_data["latitude"] = lat
form_data["longitude"] = lon
response = requests.post(
f"{API_BASE_URL}/analyze-image",
files={"file": ("crop.jpg", img_file, "image/jpeg")},
data=form_data,
timeout=60,
)
response.raise_for_status()
result = response.json()
except requests.exceptions.ConnectionError:
await msg.reply_text(s(context, "conn_error").format(url=API_BASE_URL))
return WAITING_PHOTO
except Exception as exc:
logger.error("API error: %s", exc)
await msg.reply_text(s(context, "analysis_error").format(err=exc))
return WAITING_PHOTO
finally:
Path(tmp_path).unlink(missing_ok=True)
disease = result.get("disease_name", "Unknown")
confidence = result.get("confidence", "Unknown")
remedy = result.get("remedy", "N/A")
prevention = result.get("prevention", "N/A")
alert = result.get("outbreak_alert")
report_id = result.get("report_id")
conf_emoji = {"High": "🔴", "Medium": "🟡", "Low": "🟢"}.get(confidence, "⚪")
lines = [
s(context, "diagnosis_header"), "",
f"{s(context, 'disease_label')} {disease}",
f"{conf_emoji} {s(context, 'confidence_label')} {confidence}", "",
f"{s(context, 'remedy_label')}\n{remedy}", "",
f"{s(context, 'prevention_label')}\n{prevention}",
]
if report_id:
loc_note = f" (📍 {lat:.4f}°, {lon:.4f}°)" if lat is not None else ""
lines += ["", s(context, "report_stored").format(id=report_id, loc=loc_note)]
if alert:
lines += ["", "━━━━━━━━━━━━━━━━", alert]
lines.append(s(context, "next_photo"))
await msg.reply_text("\n".join(lines), parse_mode="Markdown")
return WAITING_PHOTO
async def wrong_input_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text(
s(context, "nudge_photo"),
parse_mode="Markdown",
)
return WAITING_PHOTO
# ---------------------------------------------------------------------------
# Cancel / restart
# ---------------------------------------------------------------------------
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data.clear()
await update.message.reply_text(
s(context, "session_ended"),
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
if TELEGRAM_TOKEN == "YOUR_BOT_TOKEN_HERE":
raise RuntimeError("Set TELEGRAM_BOT_TOKEN before running bot.py")
application = Application.builder().token(TELEGRAM_TOKEN).build()
conv = ConversationHandler(
entry_points=[
CommandHandler("start", start),
CommandHandler("restart", start),
],
states={
WAITING_LANGUAGE: [
MessageHandler(
filters.TEXT & ~filters.COMMAND & (
filters.Regex(LANG_EN) | filters.Regex("ಕನ್ನಡ")
),
handle_language,
),
MessageHandler(filters.TEXT & ~filters.COMMAND, wrong_language),
],
WAITING_LOCATION: [
MessageHandler(filters.LOCATION, handle_location),
MessageHandler(filters.TEXT & ~filters.COMMAND, wrong_input_location),
],
WAITING_PHOTO: [
MessageHandler(filters.PHOTO, handle_photo),
# Allow location refresh mid-session
MessageHandler(filters.LOCATION, handle_location),
MessageHandler(filters.TEXT & ~filters.COMMAND, wrong_input_photo),
],
},
fallbacks=[CommandHandler("cancel", cancel)],
allow_reentry=True,
)
application.add_handler(conv)
logger.info("CropRadar bot starting (bilingual)…")
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()