aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJinwei Zhao <[email protected]>2017-01-20 17:49:50 +0800
committerJinwei Zhao <[email protected]>2017-01-20 17:49:50 +0800
commit8a1af78b809716a471d1c0d12cfcf89d2e19070f (patch)
treee88261931a189cd75d6fb1234937f23eaa477c15
parent4f4590b9a0b384b141d3c07caa9a500805258265 (diff)
downloadCOMM2TG-8a1af78b809716a471d1c0d12cfcf89d2e19070f.tar.gz
add search bot
-rw-r--r--searchbot.py32
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 @@
1from pymongo import MongoClient
2from telegram.ext import Updater, CommandHandler
3import logging
4# Enable logging
5logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
6 level=logging.DEBUG)
7logger = logging.getLogger(__name__)
8def 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))
18def start(bot, update):
19 bot.sendMessage(update.message.chat_id, text="Hi! Use /search agentid to start, no need for @")
20def error(bot, update, error):
21 logger.warn('Update "%s" caused error "%s"' % (update, error))
22def 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()
31if __name__ == "__main__":
32 main()
Powered by cgit v1.2.3 (git 2.41.0)