What are Telegram Client Messaging limits? - python

From here I know that Telegram limits BOT messages like this:
> 1msg/second per chat
> 30msg/second different chats
Happens that I'am not using the python-telegram-bot API, I'am using a normal client through telethon's library. I'am getting Flood\Spam error from Telegram when trying to send messages in that cadence. BTW, I'am using delayQueue Class to control the messages limits like this:
dqueue = DelayQueue(burst_limit=29, time_limit_ms=1014)
dqueue(client.send_message,inputPeerUser,msg)
Already asked directly to telegram's support, but got no answer. Does someone know what are the limits to normal clients?

There is a limit of 30 msg/sec.
Also,a bot can use the API requests for a limited no. of times
I get this error when I usually test the bot.During testing,there would be a lot of scenarios to test.So,I would be sending a huge amount of requests.
I get an error like this, {error_code:429,description:Too many requests try after (some_time)}
Idk the exact limit.
To avoid this:
You should not flood the telegram server with requests

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

change messages in ms teams chat

I am using the library to send messages to the chat.
import pymsteams
myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")
myTeamsMessage.title("Title")
myTeamsMessage.text("Text")
myTeamsMessage.send()
How can I access the sent message so that it can be changed over time.
Using that library you can't update already sent messages, it uses Teams Webhooks and only sends messages.
Seems like you need https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/update-and-delete-bot-messages?tabs=dotnet
As far as I can tell, this library does not support modifying your message but you can access what you sent by using myTeamsMessage.payload["text"].

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

How to export Slack messages to Python

I've been trying to use outgoing webhook in Slack to export messages from the channel to my Python program but I can't find the way to do it, so I wonder if even a thing like this exists?
Slack seems to have an API you can use to retreive the messages of a given channel.
Follow this link : https://api.slack.com/messaging/retrieving
You can make a GET request to retreive the full history of the given conversation (so all the messages) :
GET https://slack.com/api/conversations.history?token=YOUR_TOKEN_HERE&channel=CONVERSATION_ID_HERE
Content-type: application/json
You can easily make GET requests wiht python using the library called 'requests'.
If you're able to have a valid API token and the ID of the conversation, you will then be able to get all the messages of the chosen conversation.

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