JungDev / django-telegrambot

Simple app for Telegram bot in Django
BSD 3-Clause "New" or "Revised" License
237 stars 88 forks source link

Sending message with bot #18

Open Katerou22 opened 6 years ago

Katerou22 commented 6 years ago

How to send message with bot without handler in this package?!

something just like this: bot.sendmessage().... bot out of any def!

JungDev commented 6 years ago

You may just obtain a bot from DjangoTelegramBot.getBot() method. If you have more than one bot in your config, you could select it by token or username with bot_id parameter in the call.

example how use a bot in a views:

from django.http import HttpResponse
from django_telegrambot.apps import DjangoTelegramBot

def sendMessageTo(request, chat_id, msg_text):    
    bot = DjangoTelegramBot.getBot(bot_id="myBot")
    bot.sendMessage(chat_id, text=msg_text)
    response = "Message send to %s."
    return HttpResponse(response % chat_id)
ChD1 commented 5 years ago

I use this part of code in celery periodic task:

        bot = DjangoTelegramBot.getBot('bot_name')
        bot.sendMessage(chat_id, "Hello")

But i see only this error: AttributeError: 'NoneType' object has no attribute 'sendMessage' How to fix?

YazdanRa commented 4 years ago

I see this error too: AttributeError: 'NoneType' object has no attribute 'sendMessage'

lukruh commented 4 years ago

It means that the first line does not return a valid bot object. The required settings (i.e. token) must be defined in djangos settings.py and you have to make sure, that this settings are loaded, when the script is called from something else than django/wsgi. The 'bot_name' string need to be replaced with the bots user name (not display name!), which belongs to the token given in settings.py.