def serve(bot, update_id, data): interface = telegram.ReplyKeyboardMarkup( [["Arabic", "Audio", "English", "Tafsir"], ["Previous", "Random", "Next"]], resize_keyboard=True ) def send_quran(s, a, quran_type, chat_id, reply_markup=None): if not (0 < s < 115 and 0 < a <= Quran.surah_lengths[s]): bot.sendMessage(chat_id=chat_id, text="Ayah does not exist!") return elif quran_type in ("english", "tafsir"): text = data[quran_type].getAyah(s, a) bot.sendMessage(chat_id=chat_id, text=text, reply_markup=reply_markup) elif quran_type == "arabic": bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO) image = "quran_images/" + str(s) + "_" + str(a) + ".png" upload_file(image, quran_type, chat_id=chat_id, caption="Quran %d:%d" % (s, a), reply_markup=reply_markup) elif quran_type == "audio": bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.UPLOAD_AUDIO) audio = "Husary/" + str(s).zfill(3) + str(a).zfill(3) + ".mp3" upload_file( audio, quran_type, chat_id=chat_id, performer="Shaykh Mahmoud Khalil al-Husary", title="Quran %d:%d" % (s, a), reply_markup=reply_markup, ) save_user(chat_id, [s, a, quran_type]) def upload_file(filename, quran_type, **kwargs): def upload(f): if quran_type == "arabic": v = bot.sendPhoto(photo=f, **kwargs)["photo"][-1]["file_id"] elif quran_type == "audio": v = bot.sendAudio(audio=f, **kwargs)["audio"]["file_id"] save_file(filename, v) def upload_from_disk(): with open(filename, "rb") as f: upload(f) f = get_file(filename) if f is not None: try: upload(f) except telegram.TelegramError as e: if "file_id" in e.message: upload_from_disk() else: raise e else: upload_from_disk() for update in bot.getUpdates(offset=update_id, timeout=10): chat_id = update.message.chat_id update_id = update.update_id + 1 message = update.message.text.encode("utf-8").lower() state = get_user(chat_id) if state is not None: s, a, quran_type = state else: s, a, quran_type = 1, 1, "english" print("%d: %s" % (chat_id, message)) if chat_id < 0: continue # bot should not be in a group if message.startswith("/"): command = message[1:] if command in ("start", "help"): text = ( "Send me the numbers of a surah and ayah, for example:" " 2 255. Then I respond with that ayah from the Noble " "Quran. Or type: random." ) elif command == "about": text = ( "The English translation is by Imam Ahmed Raza from " "tanzil.net/trans/. The audio is a recitation by " "Shaykh Mahmoud Khalil al-Husary from everyayah.com. " "The tafsir is Tafsir al-Jalalayn from altafsir.com." "The source code of the bot is available at: " "https://github.com/rahiel/BismillahBot." ) else: text = "Invalid command" bot.sendMessage(chat_id=chat_id, text=text) continue match = re.match("(\d+)[ :\-;.,]*(\d*)", message) if match is not None: s = int(match.group(1)) a = int(match.group(2)) if match.group(2) else 1 send_quran(s, a, quran_type, chat_id, reply_markup=interface) elif message in ("english", "tafsir", "audio", "arabic"): send_quran(s, a, message, chat_id) elif message in ("next", "previous", "random"): if message == "next": s, a = Quran.getNextAyah(s, a) elif message == "previous": s, a = Quran.getPreviousAyah(s, a) elif message == "random": s, a = Quran.getRandomAyah() send_quran(s, a, quran_type, chat_id) sys.stdout.flush() return update_id
def serve(bot, data): global update_id def send_quran(s, a, quran_type, chat_id, reply_markup=None): if quran_type in ("english", "tafsir"): text = data[quran_type].getAyah(s, a) bot.sendMessage(chat_id=chat_id, text=text[:MAX_MESSAGE_LENGTH], reply_markup=reply_markup) elif quran_type == "arabic": bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO) image = get_image_filename(s, a) send_file(bot, image, quran_type, chat_id=chat_id, caption="Quran %d:%d" % (s, a), reply_markup=reply_markup) elif quran_type == "audio": bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.UPLOAD_AUDIO) audio = get_audio_filename(s, a) send_file(bot, audio, quran_type, chat_id=chat_id, performer="Shaykh Mahmoud Khalil al-Husary", title="Quran %d:%d" % (s, a), reply_markup=reply_markup) save_user(chat_id, [s, a, quran_type]) for update in bot.getUpdates(offset=update_id, timeout=10): update_id = update.update_id + 1 if update.inline_query: query_id = update.inline_query.id query = update.inline_query.query results = [] cache_time = 66 * (60 ** 2 * 24) s, a = parse_ayah(query) if s is not None and Quran.exists(s, a): ayah = "%d:%d" % (s, a) english = data["english"].getAyah(s, a) tafsir = data["tafsir"].getAyah(s, a) results.append(InlineQueryResultArticle( ayah + "english", title="English", description=english[:120], input_message_content=InputTextMessageContent(english)) ) results.append(InlineQueryResultArticle( ayah + "tafsir", title="Tafsir", description=tafsir[:120], input_message_content=InputTextMessageContent(tafsir)) ) else: results = data["default_query_results"] bot.answerInlineQuery(inline_query_id=query_id, cache_time=cache_time, results=results) continue if not update.message: # weird Telegram update with only an update_id continue chat_id = update.message.chat_id message = update.message.text.lower() state = get_user(chat_id) if state is not None: s, a, quran_type = state else: s, a, quran_type = 1, 1, "english" print("%d:%.3f:%s" % (chat_id, time(), message.replace('\n', ' '))) if chat_id < 0: continue # bot should not be in a group # "special:quran_type" special_state = quran_type.split(':') if message.startswith('/'): command = message[1:] if command in ("start", "help"): text = ("Send me the numbers of a surah and ayah, for example:" " <b>2:255</b>. Then I respond with that ayah from the Noble " "Quran. Type /index to see all Surahs or try /random. " "I'm available in any chat on Telegram, just type: <b>@BismillahBot</b>") elif command == "about": text = ("The English translation is by Imam Ahmed Raza from " "tanzil.net/trans/. The audio is a recitation by " "Shaykh Mahmoud Khalil al-Husary from everyayah.com. " "The tafsir is Tafsir al-Jalalayn from altafsir.com." "The source code of BismillahBot is available at: " "https://github.com/rahiel/BismillahBot.") elif command == "index": text = data["index"] elif command == "feedback": text = ("Jazak Allahu khayran! Your feedback is highly " "appreciated and will help us improve our services. " "Your next message will be sent to the developers. " "Send /cancel to cancel.") save_user(chat_id, (s, a, "feedback:" + quran_type)) elif command == "cancel": text = ("Cancelled.") if len(special_state) > 1: save_user(chat_id, (s, a, special_state[1])) else: text = None # "Invalid command" if text: bot.sendMessage(chat_id=chat_id, text=text, parse_mode="HTML") continue if len(special_state) > 1: if special_state[0] == "feedback": with open("feedback.txt", 'a') as f: f.write("%d: %s\n" % (chat_id, message)) text = "Feedback saved 😊" bot.sendMessage(chat_id=chat_id, text=text) save_user(chat_id, (s, a, special_state[1])) continue if message in ("english", "tafsir", "audio", "arabic"): send_quran(s, a, message, chat_id) continue elif message in ("next", "previous", "random", "/random"): if message == "next": s, a = Quran.getNextAyah(s, a) elif message == "previous": s, a = Quran.getPreviousAyah(s, a) elif message in ("random", "/random"): s, a = Quran.getRandomAyah() send_quran(s, a, quran_type, chat_id) continue s, a = parse_ayah(message) if s: if Quran.exists(s, a): send_quran(s, a, quran_type, chat_id, reply_markup=data["interface"]) else: bot.sendMessage(chat_id=chat_id, text="Ayah does not exist!") sys.stdout.flush()