Python Telegram Bot delete_message (aiogram) - python

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

Related

Getting content from a dm in discord.py

So I want to know if it is possible, that a bot gets the content sent to it in a dm and send that in a specifyed channel on a server.
So basically you dm the bot the word "test" and the bots sends the word in a channel of a server
Yes, it is possible for a bot to receive a direct message and then repost the message in a specified channel on a server. This can be done using the Discord API.
You can do the following:
Create a Discord bot and add it to your server. You can do this using the Discord developer portal.
Use the Discord API to listen for messages sent to the bot in a DM. You can do this using the message event and the DMChannel class in the Discord API.
When the bot receives a DM, use the Discord API to repost the message in the specified channel on the server. You can do this using the send method of the TextChannel class in the Discord API.
Yes, this is possible:
First, we use the on_message() event to detect when a message is sent.
We then check if the message is sent in a DM.
If so, we will send a message to a specific channel.
Here is one way you can implement it:
# import ...
bot = discord.Bot(...)
#bot.event
async def on_message(message):
# check if it's
if isinstance(message.channel, discord.DMChannel):
# get the channel from ID
channel = client.get_channel(CHANNEL_ID) # put channel ID here
await channel.send("test") # whatever you want to send
#bot.event:
async on_message(message):
if message.DMChannel:
# define channel and send message to channel.
You can also refer to DOCS.

Why am I author of message send by byt - python discord

I am trying to send discord message to channel using python bot, but when I print it's author, it's me and not the bot. So later I can't edit it because of the author.
How can I send message as the bot?
My function:
#bot.command(name="send")
async def send(ctx: Context) -> None:
message = "message"
await ctx.channel.send(message)
print(ctx.message.author)
Because ctx.message is the message that invoked the command, not the message sent by the bot... So ctx.message.author is the person that invoked the command, which is you.
print(ctx.message.author) # <- "print the author of the message that invoked this command"
When you use Messageable.send(), it returns the message it sent. Thus, ctx.channel.send() returns the message that it sends. The .author of that message is your bot, and that is the message you can edit.
message = await ctx.channel.send("something")
I should mention I'm using a wrapper called pycord which I highly recommend.
member.id (Shows the specified members id)
member.name (Shows the specified members name and #)
member.mention (Mentions the specified member)
Doing ctx.send will show only the bot's message but will show a error code at the same time, So use ctx.respond("whatever") instead.
ctx.channel.send will send to a channel is
channel.send will send to a specified channel

How to get an old message ID with python telegram bot?

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)

Send message to dms discord.py

I want to send message to dms using my discord bot and because the api has changed from client.send_message(user, message) to channel.send(message) I have no idea how to do that, also I dont want to make it dependent on the on_message(message) event. Any ideas?
If you have the user ID you can do:
user = client.get_user(user_id)
await user.send('Hello')
However If you already have the User object, you can do the following:
await message.author.send('Hey')
Where message is a Message object received from a on_message() event.
As for whether you can send private messages without first receiving an event unfortunately that is not be possible due to obvious spam-related issues.

How to get new message received from the telegram channel using telethon

I used the code given here to receive new message from the user but it does not work when a new message arrives in the telegram channel.
#bot.on(events.NewMessage)
async def my_event_handler(event):
print(event.stringify())
Setting events.NewMessage(chat='chat') or events.NewMessage(chat='channel') didn't work.
How can a telegram bot get new message event from a telegram channel ?
For a bot to receive all messages, you first need to configure it in #BotFather by disabling the bot privacy:
/start
/mybots
(select a bot)
Bot Settings
Group Privacy
Turn off
With that done, add the bot as admin to your broadcast channel (they can't be normal members here). Your code should look like this:
CHANNEL = ... # id, username or invite link of the channel
# the first parameter is the `chats=`, you can use a named argument if you want
#bot.on(events.NewMessage(CHANNEL))
async def my_event_handler(event):
print(event.stringify())
If you want to handle messages from all broadcast channels your group is in, use a more advanced filter:
# megagroups (supergroups) are channels too, so we need `not e.is_group`
# this lambda takes the event, which has these boolean properties
#bot.on(events.NewMessage(func=lambda e: e.is_channel and not e.is_group))
async def my_event_handler(event):
print(event.stringify())
If you only want to get the message text instead of the entire json, you can try this
print(event.message.message)

Categories

Resources