Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending message with bot #18

Open
Katerou22 opened this issue May 13, 2018 · 4 comments
Open

Sending message with bot #18

Katerou22 opened this issue May 13, 2018 · 4 comments

Comments

@Katerou22
Copy link

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

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

@JungDev
Copy link
Owner

JungDev commented May 16, 2018

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
Copy link

ChD1 commented Aug 29, 2019

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
Copy link

YazdanRa commented Jul 7, 2020

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

@lukruh
Copy link
Contributor

lukruh commented Jul 13, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants