diff options
-rw-r--r-- | searchbot.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/searchbot.py b/searchbot.py new file mode 100644 index 0000000..e18875f --- /dev/null +++ b/searchbot.py | |||
@@ -0,0 +1,32 @@ | |||
1 | from pymongo import MongoClient | ||
2 | from telegram.ext import Updater, CommandHandler | ||
3 | import logging | ||
4 | # Enable logging | ||
5 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
6 | level=logging.DEBUG) | ||
7 | logger = logging.getLogger(__name__) | ||
8 | def search(bot, update, args): | ||
9 | agentid = args[0] | ||
10 | print(agentid) | ||
11 | conn = MongoClient('Mongo Address') | ||
12 | db = conn['COMM_Hangzhou'] | ||
13 | collection = db.entries | ||
14 | res = collection.find({"$text": {"$search": agentid}}) | ||
15 | for i in res: | ||
16 | print(i) | ||
17 | bot.sendMessage(update.message.chat_id, text=str(i)) | ||
18 | def start(bot, update): | ||
19 | bot.sendMessage(update.message.chat_id, text="Hi! Use /search agentid to start, no need for @") | ||
20 | def error(bot, update, error): | ||
21 | logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
22 | def main(): | ||
23 | updater = Updater('Telegram Bot ID') | ||
24 | dp = updater.dispatcher | ||
25 | dp.add_handler(CommandHandler("start", start)) | ||
26 | dp.add_handler(CommandHandler("help", start)) | ||
27 | dp.add_handler(CommandHandler("search", search, pass_args=True)) | ||
28 | dp.add_error_handler(error) | ||
29 | updater.start_polling() | ||
30 | updater.idle() | ||
31 | if __name__ == "__main__": | ||
32 | main() | ||