I am writing a telegram bot to handle conversations (one to one) with users and I am getting same for both userid and chat_id.
My code is:
chat_id = update.message.chat_id
user_id = update.effective_user.id
user_id is the Telegram ID of the user sending a message to bot.
chat_id is the Telegram ID of the chat where a message is being sent to bot.
So, in one to one conversation, the user will send a message in his own chat (with respect to bot). That's the reason both are same.
Related
How to get user_id who left the reaction or comment the post and send him message?
I need to delete messages sent by the bot, and to achieve this I have to use the parameters chat_id and message_id.
For chat_id I easily use message.chat.id
, but I don't know how to get message_id
If I use
bot.send_message(message.chat.id, 'Hello!')
How can I get message_id from this message, or how can I delete it
according to the telegram api the send message function returns an updates value, which you can use to get the message id
I have a Telegram channel with a bot inside it, which I use to send messages to the channel and edit them with this Python code
import telegram
bot = telegram.Bot(token=MY_TOKEN)
msg = bot.send_message(chat_id=MY_CHANNEL_ID, text="...") # send a message
msg = msg.edit_text("...") # edit the previous message
Sometimes the python bot crashes/closes for whatever reason, when I start the bot again how to recover the ID of a message sent during the previous session so that I'd be able to edit the message again?
I read about this method
bot.edit_message_text(text, chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id)
I guess that for chat_id I have to use MY_CHANNEL_ID but what I don't understand is how to get the message_id of the old message
MANUAL SOLUTION
I found out that the message_id is just the number contained in the post link, so a workaround is to manually copy the post link of a message from the telegram client, for example if the post link is https://t.me/my_channel_name/207 then the message_id is 207 and to edit that message we have to run
bot.edit_message_text(text="...", chat_id=MY_CHANNEL_ID, message_id=207)
I have a invite link of private channel, and I wanna forwards (or delivery) posts from this channel to me. My desired pseudo code is like below.
def get_channel(bot, update):
message=update.channel_post.text
print(message)
updater = Updater(my_token)
channel_handler = MessageHandler(Filters.text, get_channel,
channel_post_updates=True, invite_link='http://t.me/aa23faba22939bf')
updater.dispatcher.add_handler(channel_handler)
This works well when my bot is in the channel which I created(invite_link is added for my purpose. I don't know where invite_link should be input). But what I want is that my bot forwards posts from channel in which my bot is 'not' included. I prefer python, but any API will be ok. I searched all the google world but have no clues.. Any tips appreciated.
I found a solution with Telethon library. It works for me(http://telethon.readthedocs.io/en/latest/extra/advanced-usage/update-modes.html)
def callback(update):
print('I received', update)
client = TelegramClient('session', api_id, api_hash,
update_workers=1, spawn_read_thread=False)
client.connect()
client.add_event_handler(callback)
client.idle() # ends with Ctrl+C
In callback function you can filter only channel posts or group messages.
If you want to forward message (of any kind) from channel (doesn't matter if it's private or public) to any other chat:
add your bot to channel (notes that, bots are only added as an admin to channel and have admin privileges)
following code (in python) will do the Forwarding:
from telegram import Bot
def forward(update, context):
chat_id = update.effective_chat.id
bot = Bot(token=TOKEN)
bot.forward_message(chat_id = chat_id,
from_chat_id = channel_id,
message_id = (number))
the above code, will forward message from 'from_chat_id' to 'chat_id' (is the user's chat_id which ask the query)
channel_id is just simply the channel's telegram invite link (which start with #)
message_id is the unique number of the message which is in the channel. If you right-click on the message inside the channel, and select 'Copy Post Link' and paste it somewhere, is something like this:
https://t.me/c/123456789/2040
(123456789 is the link of private channel. If this was a public channel, instead of '123456789', there was channel's public address which starts with #)
the message id is: 2040, not the whole.
refrence: https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html#telegram.Bot.forward_message
I create a bot for telegrams,
he should collect the id of everyone who wrote in the group chat for a certain time, but I can not extract the user's id from the messages, instead I get only the chat id
The question is how to me from messages sent to the chat to pull only the id of the user who sent the message.
to create a bot I use a python, please, give examples only on it.
#bot.message_handler(commands=['MyID'])
def get_id(msg):
bot.send_message(msg.chat.id, "ID USER\n")
bot.send_message(msg.chat.id, msg.chat.id)