This is my code
from telegram.ext import Updater, CommandHandler
import os
from pymongo import MongoClient
TOKEN = 'TOKEN'
def get_db(update, context):
cluster = MongoClient("mongodb+srv://testing:[email protected]/test?retryWrites=true&w=majority")
result = list(cluster.get_database('DBNAME')['COLLECTIONNAME'].find({}))
update.message.reply_text(str(result))
def main():
updater = Updater(TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("getdb", get_db))
updater.start_webhook(listen="#.#.#.#",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook('https://MYHEROKUAPP.herokuapp.com/' + TOKEN)
updater.idle()
if __name__ == '__main__':
main()
Everytime I type /getdb
, the bot doesn’t give me any response. When I tried several experiments, Seems there’s some Error on cluster variable. I used try except syntax, but the bot didn’t show anything, even from the except and I couldn’t found the Error name as well. And I’m using heroku server for the telegram bot. How to fix this?
2
Answers
You can connect to default db (which is the one defined in the connection string) and query the collections like this
I’m not sure if you still have this issue, but the code seems to be okay so far.
Try logging the app info to the terminal to get a better idea of what the error is.