Cannot "send_message" in Telegram Bot API - python

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...

Related

How can I monitor (test) and view the DMs my Slack Bot is sending?

I've created a slack bot that sends out a private message via the client.chat_postMessage method to different users in my workspace.
They've received the message (they showed me screenshots), however, I cannot see their private conversation via Slack it seems. Is there another way to monitor that the message has arrived and looks the way it's supposed to be (the format, attachments, etc. sent together)?
I tried this method:
response = client.conversations_info(
channel=,
include_num_members=1
)
but when I input the user's channel it returns that the channel doesn't exist.
Here's what my bot's message looks like in Slack according to the receiver. I cannot view it anywhere it seems.
If you're using your bot token to send a message to a user, the message will show up in a DM between your bot and that particular user. Your bot is its own user, separate from your user. For that reason, if you login to Slack, you won't be able to view that conversation as you yourself are not a part of the conversation.
The only way to "view" a message that has been sent to a user within a DM is to call the conversations.history method. Use the channel ID of the DM between the bot and your user, which will start with "D".
If you want to confirm how a message will look like within Slack, send the message to yourself beforehand. Additionally, adding logging within your code will help for any troubleshooting that you may need in the future.

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

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)

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).

discord.py emoji all servers bot in

I have a bot that sends embeds that include emojis specific to each embed and it works great on my test server where I uploaded the emojis but they don't show up on other servers since those servers don't have the custom emoji.
using:
emoji = get(ctx.message.guild.emojis, name='emojinamehere')
works of course for the server it's in but is there a way to get and use all emojis from any servers the bot is in?
Because you're specifically using ctx.message.guild.emojis aka you're trying to get the emoji from the context guild (which might not have it).
You can get rid of get finding by name and just use bot.get_emoji() where bot is your bot/client object.
Your bot will store all emojis from all the guilds in internal cache and get_emoji() will retrieve it from there so it will work for all guilds.
You will need emoji ID (integer) to do this, not name as you're currently getting. You could get by name and then get its ID but that's just unnecessary steps.
Just use emoji ID as those are unique and can't change (names can both be duplicate and can change so you can get into problems getting by name).
You can quickly get emoji ID by using Discord client app:
enter custom emoji in the text box input
enter \ before emoji
send message to chat
message will transfom into somemthing like <:emoji_name:emoji_id>
Yes, this is necroposting, but this may help for anyone looking for the answer.
#client.command()
async def emoji(ctx, emojiname):
for i in client.guilds:
emoji = discord.utils.get(i.emojis, name=emojiname)
Of course you have to pass the emoji's name as the first argument.

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

Categories

Resources