How to send a message to the user in a Telegrame from the bot?

1) Created bot
2) Initiated a conversation with him
3) Make a request (the token and the chat id put my)
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chatid>&text=Hello</chatid></token>


You get a response {"ok":false,"error_code":403,"description":"Forbidden: bot can't initiate conversation with a user"}

Tell me what's wrong.
June 14th 19 at 20:00
2 answers
June 14th 19 at 20:02
Solution
From the error we can see that the bot can't create the conversation. You just create it using the account and not using bot? Check carefully the variables: id of the user and so on

It's hard to say what's wrong without seeing the code... Throw it here - solve the problem)

What is the library used? Relevant? They have there the update was not so long ago, MB things have changed - in the order of complete nonsense idea.
Use telebot to python

So I get the chat id , when the user writes /start and send a direct message (it comes)
@bot.message_handler(commands=['start'])
def handle_start_help(message):
 chatId = message.chat.id
 bot.send_message(chatId, "Hello")

 #here is save the chat ID in the database


Then try again to send a message using the chat ID from the database and get the error
{"ok":false,"error_code":403,"description":"Forbidden: bot can't initiate conversation with a user"} - adaline.Wiegand66 commented on June 14th 19 at 20:05
You about pyTelegramBotAPI? From the documentation - https://github.com/eternnoir/pyTelegramBotAPI
Let's define a message handler which handles incoming /start and /help commands.
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
 bot.reply_to(message, "Howdy, how are you doing?")


Why do You need id?
Try to use a reply_to
All the functions in the post checked just now - work

ps function that responds to any message
@bot.message_handler(func=lambda m: True)
def echo_all(message):
 bot.reply_to(message, message.text)

bot.polling()


psps send a message
@bot.message_handler(func=lambda m: True)
def echo_all(message):
 chat_id = message.chat.id
print(chat_id)
 bot.send_message(chat_id, 'some text')
- sonia20 commented on June 14th 19 at 20:08
Yes about him. I need to write periodically to the user (to notify that the site is not working) - adaline.Wiegand66 commented on June 14th 19 at 20:11
Try using print(message.chat.id) view id of the chat and paste it directly. Clear chat history -> click start -> view id in the console
import telebot

TOKEN = 
bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
 bot.reply_to(message, "Howdy, how are you doing?")
print(message.chat.id)

chat_id = '105674243' # <- paste id
bot.send_message(chat_id, 'some text')

bot.polling()


ps checked again Your code(just copy and paste) - works. Or are You in another place there is an error(if it is not the whole code) or try to do update libraries - sonia20 commented on June 14th 19 at 20:14
it turned out that a typo in the token, next time I will look - adaline.Wiegand66 commented on June 14th 19 at 20:17
June 14th 19 at 20:04
Maybe not the chat_id indicate? Or a token from another bot?
And everything described in description.
The token is definitely from a bot, that's about the chat id in doubt, how can I check? - adaline.Wiegand66 commented on June 14th 19 at 20:07

Find more questions by tags TelegramPython