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

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)

Related

How can i force the Telegram Bot to write to the user first (aiogram)?

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

Why can't I send messages to Telegram bot?

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.

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.

Telegram: Send message to bot from different script

I have a telegram bot that handles user input to perform actions on links that are sent.
In my Telegram Bot I have:
def handle_message(bot, update):
url = update.message.text
# do parsing and add url to database
dispatcher.add_handler(MessageHandler(filters=Filters.text, callback=handle_message))
However I want to also be able to send a URL to the bot from a POST request. i.e. use the telegram bot's API to send a message to itself so it can parse the link.
How can I send a POST request to my telegram bot and have it run handle_message() on the input?
It is technically impossible for bots to communicate with each other (and therefore with themselves).

send direct message to slack user from a app bot but not in app channel

Is there a way to send direct message to from a bot(myapp/user) to a user say xyz or to a user's slackbot. The message needs to appear against the user and not on the mybot app.
I am using the python slack-client.
Using below code to send the message:
user_id="<touser>"
im_channel=self.open_dm(user_id)
slack_client.api_call("chat.postMessage",channel=im_channel,text="hi buddy", as_user=True)
The above code posts the message in the myapp app channel. Is there a way for the bot to send the message directly to the user and not in the app channel?
OR
Is there a way for the myapp bot to send to slackbot channel addressing the user?
Yes.
Just send a message with the user ID for channel and it will appear in the slackbot channel of that user.
Something like this:
user_id="<touser>"
slack_client.api_call("chat.postMessage",channel=user_id,text="hi buddy")
However, note that every message on Slack must to use a channel that includes so called "direct messages". That is how Slack works.

Categories

Resources