Is it possible to download images from a private telegram channel? - python

Is it possible to download images from a private telegram channel without admin rights using Python?
Basically what I want is the script to wait till a picture is sent in the private channel and if one got sent, I want it download the picture with the filename increasing(img1, img2...). Problem is I dont have admin rights in this channel and no Bot.

Sure. If you have access to this channel, you can use the "telethon" library to implement this function.
https://docs.telethon.dev/en/stable/
The first thing you have to do is get a channel
input_entity = await UserMethods.get_input_entity(client, peer="https://t.me/example")
entity = await client.get_entity(input_entity)
and then:
client.download_profile_photo(entity, file=photo_path)

Related

How to use parse mode in telegram send message bot in Python

I'm pretty new to making bot thing so I have several questions I want to ask regarding this:
I'm using a bot to send message by
def send_message(chat_id, msg):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}"
requests.get(url)
send_message(chat_id,msg)
By this, I can send message and I want to beautify the result by using parse_mode but I don't know where to put in in url to make it work.
I'm alternate to using Telethon. I can use it to send message to individual and group by user name and group invite link, but when I try to send it to channel by:
client.send_message(entity='my channel name', message=message)
When I try to run it, it return Cannot find any entity corresponding to "Test channel".
I also try
destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")
but it require my channel access_hash, how can get it, or are there any other way to send message to channel.
I know Telegram bot API have function like sendMessage or bot.sendMessage that also can do the job, but somehow I can't call it, which packages should I install and/or import.
Thanks a lot!
This should do the trick:
def send_message(chat_id, msg, parse_mode):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}&parse_mode={parse_mode}"
requests.get(url)
TBH I'm not too familiar with telethon, but that library is mainly intended for so called userbots. that means that it uses the API that also the Telegram app uses and because of that, it's often times more involved than just the plain bot api.
To use the Bot API in Python, I can recommend the package python-telegram-bot, see https://python-telegram-bot.org. Disclaimer: I'm currently the maintainer of that package. There are also other python packages for the bot api, see e.g. https://core.telegram.org/bots/samples#python

Telegram Bot delete sent photos?

I am looking to delete a photo sent by my bot (reply_photo()), I can't find any specific reference to doing so in the documentation, and have tried delete_message(), but don't know how to delete a photo. Is it currently possible?
It's currently possible in Telegram API, not the Bot API unfortunately. It's a shame :(
I found a workaround (that I am currently still playing with). I named the photo I sent, then when I want to delete it, I delete it using .delete(). For example:
photo = x.message.reply_photo(...)
...
...
photo.delete()
You need to have the chat_id and the message_id of that message sent by bot, then you can delete using context.bot.delete_message(chat_id, message_id).
Note: Bot cannot delete a message if it was sent more than 48 hours ago.

Get last message/s from Telegram channel with Python

I'm using the python-telegram-bot library to write a bot in Python that sends URLs into a channel where the bot is administrator.
Now, I would like to have the bot reading, let's say, the last 5 messages (I don't really care about the number as I just need to read the message on the chat) and store them into a list in the code for further elaborations.
I already have my bot working with:
bot = telegram.Bot(token='mytoken')
bot.sendMessage(chat_id='#mychatid', text=entry.link)
But I can't find a bot.getLastMessage or bot.getMessage kind of class into the python-telegram-bot library.
In case there's already no written class that does that, how can I implement it via the Telegram API as I'm a bit of a beginner when it comes to API implementation?
Thanks.
That's not possible in Bots unfortunately.
Here you can find all available methods (that python-telegram-bot invokes behind the scenes) and there's no such method available to fetch messages on demand.
The closest you can get through the api is getChat (which would return the pinned_message in that chat).
What you can do in this case is, store the messages the bot sends as well as the message updates the bot receives (by setting up a handler) in some storage (database) and fetch from there later on.
Have you tried the other type of Telegram API, Telegram [client] API and TDLib?
Using telethon library makes it easy to read channels history (see telethon docs).
For this, we need an api_id and an api_hash.
To get these parameters, we need to log in to our Telegram core
and go to the API development tools area.
There is a simple form that needs to be filled out, after which, we can receive our api_id and api_hash. See Telegram's help documentation about how to get your API credentials.
Here is an example code that gets the last 5 messages of targetChannelId:
from telethon import TelegramClient
API_ID = 123456 # See above for how to get it
API_HASH = '123abc' # See above for how to get it
client = TelegramClient('my-client', API_ID, API_HASH)
async def main():
async for message in client.iter_messages('targetChannelId', limit=5):
print(message.id, message.text)
with client:
client.loop.run_until_complete(main())
The first time you run this code it asks your phone number or bot token. Enter your phone number in the format +9912345... where 99 is your country code and the rest is your phone number.
It then may send a login code to your Telegram app; enter it in the console.
Note: Bots cannot see channel history messages (at least in telethon) but users (phone numbers) can. Bots can only listen for channel updates only if they are one of its administrators.
The client.iter_messages() accepts other parameters like min_id which can be used so we get messages only after a specific message (for example, we can save the last message id that we have processed and next time pass that id as min_id so only messages after that message are returned).

Using the Slack API, how can I direct message all users at the same time?

On Slack, I'm aware that using chat.postMessage allows me to message each user individually, but how would I go about direct messaging the entire team (400 members) at once?
There is no "bulk" variant of chat.postMessage. So you basically need to build your own bulk message sender, which you can easily do by iterating through the list of users and sending each of them a message.
You can get the list of all users with users.list. You then have two option for sending direct messages:
Slackbot channel: Send each of them a message with chat.postMessage using the ID of each user as channel.
App channel: Get the IM channel ID with im.open for each user
and then use that as channel for chat.postMessage. This only works though if you have a bot user and send the message from the bot user.
Keep in mind though, that there is a request limit of 1 message per second.
There also is a 3 seconds request time-out for many requests between Slack and your app. (e.g. for direct response to slash commands). So if your bot needs to send many messages you want to use an approach that allows you to send them asynchronously.
One solution to this problem that works very well for me is to use a messaging queue for all Slack messages sent from my bots.
Hello For that you need a channel with all 400 of them in it, cause currently you cannot send to 400 individual users. For sending message to a channel, you just need to add channel argument for postMessage method.
Check this :: https://api.slack.com/methods/chat.postMessage

Cannot "send_message" in Telegram Bot API

In my own library I'm trying to send a message using "sendMessage" API method from Telegram Bot API. I've create a channel titled, say, "my_channel123" and my user name is "my_username123". So I'm an admin of the channel and the only user.
When I'm trying to send a message to the whole channel or myself from a bot, I get an error Bad request 400. Here's how I'm trying to do that:
my_bot.send_message(chat_id="#my_channel123", text="tetfdsfd")
# or
my_bot.send_message(chat_id="#my_channel123my_username123", text= "tetfdsfd")
# or
my_bot.send_message(chat_id="#my_username123", text="tetfdsfd")
I believe the error is somewhere in the format of the ids of the channel or user name or both. Are all these 3 calls correct, meaning is the format I use for chat_id correct?
Note that most likely the probable case is the id of the chat or user_name (or rather, the format) or something else because other post and get methods in my library work properly.
You have to add the bot to be an administrator of the channel before it can send messages to the channel. After you do that, your first line should work:
my_bot.send_message(chat_id="#my_channel123", text="tetfdsfd")
Also remember that whatever follows the # should be the channel username, not the title.
when sending a message to a channel, i think its easiest to send it with the channel id...
there are a few ways to get the channel id
if the bot is a admin in the channel (go to "add admin..." and search for your bot to add it), then any message sent in the channel, the bot would be able to see it, and in every message there is an id of the sender, if its in a channel, its the channel id...
use this bot #ChannelIdBot... though the bot will still need to be in the channel as an admin, in order to send messages...

Categories

Resources