Sending Telegram messages with Telethon - telethon

How to automatically send messages with a picture to all chat, channels in which you are a member? Or by the list of chats from the file. And so that it does not send it to ordinary users who write to you. And you could set the delay time for sending messages.

The first three lines of the documentation show how to send a message:
from telethon.sync import TelegramClient, events
with TelegramClient('name', api_id, api_hash) as client:
client.send_message('me', 'Hello, myself!')
This answers the question in the title. For the rest, see client.iter_dialogs, the Dialog type, and client.send_file. All links have examples.

Related

Telegram bot doesn't recieve messages from a channel

Private settings are turned off.
the bot is added to the channel and he is an admin.
i created another channel because maybe the other one had problems but it didn't helped too.
And it still can't see the messages i send in the channel , why?
(i tested it and he gets the messages everywhere except of channels)
consider that channel_post is a different update parameter than regular message parameter which is only for private chats and groups.
check more in: https://core.telegram.org/bots/api#getting-updates

How to delete messages posted to slack after sometime?

I have a slack bot that is sending direct messages to users. I want to add a functionality that will delete the message automatically 10 minutes after it has been sent. Please help.
There is an api that is used to delete chat messages :
https://api.slack.com/methods/chat.delete
For your use case, you need to capture the channel & timestamp details from the response of message that was sent. Once the message is sent, wait for required time and then call 'chat.delete' api to delete the message.

how do I retrieve the latest telegram bot messages posted in a channel through the api?

I want to see the last post a bot makes to my channel through the API/https
I tried getUpdates and it only shows messages I sent to the bot. I'd like to see messages the bot broadcasts to my channel
https://api.telegram.org/botxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getUpdates
The getUpdates method only retrieves messages that are sent to the bot by normal users. Bots cannot see other bot messages, that means it cannot see its own messages. However you can track messages sent by the bot in your code logic. E.g. in Node.js you would do something like:
bot.sendMessage(channelId, "The message sent to the channel").then(ctx => {
// ctx contains the details of the message sent to the channel
// you can do whatever you want with it
});

Telegram Bot - Send Info message

currently I am working to creating telegram bot. Now I required to know is there is any API to send info messages (like the one we get during a user is added r deleted in a group chat)
Thanks in advance
What you want to do can be done using answerCallbackQuery method.
But first of all you have to create a CallbackQuery using InlineKeyboardMarkup to create inline keyboards you can follow the steps in this link.
After creating the callback query you have to answer it using one of the codes below:
if (update.CallbackQuery.Data == "CQ1")
{
await Bot.AnswerCallbackQueryAsync(update.CallbackQuery.Id,"Text",true,null, 0);
}
This way the message will look like a message box and will disappear after the user taps on OK. But if you use the code below, the message will show up and disappear automatically after a few seconds.
else if (update.CallbackQuery.Data == "CQ2")
{
await Bot.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Text", false,null, 30);
}
By the way, messages that are shown at times like adding a user to a
group are service messages and theses kinds of message can only be
sent by telegram server.
If my suggestion does not fix your issue you can use pinMessage method
that sticks a single message to the top of the page in groups and
channels. But note that you can only pin one message to a channel
or group and for pinning another message firstly you should unpin the
previous one.
Currently there is no way to send such info messages.
And in my opinion, this feature is unlikely to be added in future because:
info messages usually tell you information about your chat; they are managed by telegram servers
therefore they should not be sent by Users
bot is an instance of User

Is it possible to read bot Telegram messages

I have successfully created a bot with and am able to fetch messages from a chat using the getupdates method (long polling).
The getUpdates method is only showing user posted messages (clientside). When I post messages directly using the sendmessage method (serverside) these messages do appear in the chat, but do not in the getUpdates log.
This page https://github.com/LibreLabUCM/teleg-api-bot/wiki/Getting-started-with-the-Telegram-Bot-API#getupdates
states it logs only when "An user messages your bot, either directly or in a group." and some other ways, but the sendMessage way is not mentioned.
I've read a bit on the setwebhook method (push) but am not sure this will fix my issue.
Is this possible?
According to Bot FAQ, bots will not be able to see messages from other bots regardless of mode.
The getUpdates method shows only updates from users, not from the bot itself. This means that when you fetch the new messages with the getUpdates method, the Telegram API will list only the messages sent by the users, not the messages sent by the bot via any method (e.g sendMessage, sendPhoto ...).
To get old messages you can store the entire update (or only the parts of the update you need) for each message (even those sent by the bot with the sendMessage method) in a file or in a database and when you need an old message you can simply fetch it form the database or the files.
I managed to get the bot messages using two bots.
One does the sendMessage method and the other one does the getUpdates method.
#Giolacca9 answer inspired me to try this workaround and it works, "not from the bot itself" :)