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)
Related
I want to send Message or Media to a specific topic in a private group in Telegram using Telethon
I tried the general send message and media to the chat link "https://t.me/c/1730038294/2" where /2 is the topic link in the group.
Tried PeerChannel but it returns the total chat entity.
Here's a sample code.
async def main():
chatlink = await client.get_entity(PeerChannel(1001730038294))
await client.send_message(chatlink, message="xyz")
with client:
client.loop.run_until_complete(main())
This is getting the whole chat but not the partition of the chat (topic).
I want to send the message to one particular topic in the chat.
NOTE: Topics in groups was introduced in telegram just some days ago.
I'm trying to send welcome message to newcomers to the channel via the bot using Aiogram. However, my bot can't find the chat with the user if he has not started a conversation with the bot before. Are there any solutions of this problem?
I have already managed to handle newcomers and add ids to the db, but
`
#router.chat_member(ChatMemberUpdatedFilter(member_status_changed=(KICKED | LEFT | RESTRICTED)
>>
(ADMINISTRATOR | CREATOR | MEMBER)))
async def on_user_join(event: ChatMemberUpdated):
with grpc.insecure_channel('') as channel:
stub = sub_unsub_pb2_grpc.SubscribtionServiceStub(channel)
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(datetime.now(tz=timezone.utc))
request = sub_unsub_pb2.SubUnsubEvent(
id=str(event.new_chat_member.user.id),
channelId=str(event.chat.id),
time=timestamp,
subStatus='join'
)
response = sub_unsub_pb2.EventResponse()
stub.TriggerEvent(request)
`
But when I try to send a message, the error chat not found is thrown.
`
await support_bot.send_message(
user_id,
f'Welcome to the channel!'
)
`
Solutions to this problem:
Is to have the bot transfer a message to the user as soon as their ID
is added to the database. This way, the user will have started a
discussion with the bot, and the bot will be capable of finding the
chat when you try to send the welcome message.
Is to use the start command, so that new users can initiate the
discussion with the bot by sending /start command. Also, in the
start command handler, you can check whether the user is a new member
and send a welcome message.
Check the user's presence in the channel by using the
get_chat_member method of the bot, which returns a ChatMember
object. If the status attribute of this object
isChatMember.CHAT_MEMBER_STATUS_LEFT, then the user isn't a member
of the channel anymore and you shouldn't send the message.
Use the message event to listen for all messages transferred by the
user in the channel and check if the user is new or not and send the
welcome message accordingly
I'm using Telebot to build a simple Telegram bot. I've set up a message handler that responds to commands succesfully, but when I try to send a single message I get an error if I use the chat id (ex: 1234567890):
Error code: 403. Description: Forbidden: bot can't send messages to
bots
I get a different error when I use the user id (ex: #my_user):
Error code: 400. Description: Bad Request: chat not found
This is my code, the auth is correct:
tg_bot = telebot.TeleBot(TG_TOKEN, parse_mode='MARKDOWN')
tg_bot.send_message(chat_id=CHAT_ID_USER, text="hola test")
Is the bot's chat different to the chat I'm supposed to talk with? Any solution and details about the bot functionality will be apreciated, I'm still learning about that!
try this
import telebot
bot = telebot.TeleBot(token)
#bot.message_handler(commands=['start'])
def start(message):
bot.send_message("1395609507","Hello")
bot.infinity_polling()
If the user under this id has not started your bot, then the bot will not be able to write to him/her first. And in this case it is natural to get the error "chat not found". This error can also be observed when working with users who first used the bot and then blocked it.
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 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.