I need to send notifications through the Telegram bot for 5 days every day from the time the user started using the bot for the first time. Is it possible to record the time when the chat id was created and do personalized notifications to each user?
I don't think Telegram's Bot API exposes that info.
But, you can record the first occurrence of a new chat ID yourself to achieve this.
Depending on your stack, use case, and environment, you could either use a full-blown database, a SQLite DB (amounts to a single file), a simple key-value store or even just a text file to do this.
Related
What I'm trying to understand is how Discord.py creates and sends responses from their on_message function.
Take this as an example:
#client.event()
async def on_message(message):
print(message.content)
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Discord bots establish a websocket connection with Discord's servers, which is essentially a (secure) two-way tunnel. This means that once the connection is up, Discord can send events to you. Instead of you having to manually fetch every single channel, Discord tells you "Hey, a message was created", and the payload attached will give all the additional info about it.
The Discord (not discord.py) docs have detailed info about how everything works behind the scenes, to help the people creating the Bot libraries for you. For example, this section details which types of events Discord can send to you. To see how something is constructed, click on one of the event types and read up on the data that Discord provides.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
Not really, unless you do in fact send a GET for every single channel, which will get you ratelimited. There's really no reason to ever use only GET/POST requests (other than Webhooks, where you just send a POST with your info to send a message to a channel without a bot).
If you'd like to read up on Discord's API, the docs I linked contain a full spec of everything, so you could try to do whatever your heart desires (... and the API supports).
Recently I've been writing bot which parses site and sends message to users, whenever new information appears. Is it possible to make it on aiogram? Thank you for your answer in advance
Yes. it is.
But you have to store user_id of your users, then when "information appears" you take from your storage user_ids and send message to each user. Also it is unreal to send message for all users in one time due to telegram limits (see here).
I am building a telegram bot that will automatically add messages to preexisting menus in a bot ( created by Manybot, not with python ) once they are uploaded to a specific channel.
I spent a week or so trying to find a way to update preexisting menus but it can't manage to find a solution.
even when I try to run an infinite loop to monitor any messages to the bot ( using any telegram-python library out there ) i get this error :
Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first
and i am forced to delete the webhook , which will in turn disable every command i added to the menus and need to create another bot
I would really appreciate any help .
The problem is not related to existing webhooks, it's the fact that each bot token can poll / webhook only once.
In other words, you can't have two scripts running with the same bot token, regardless if it's long polling or a webhook.
This is a limitation of the Telegram Bot API.
You can only run your own script when the manybot version of your bot is paused, stopped, turned off, etc. The getUpdates method will reject new connections with the same bot token.
You can either use a second bot token to use other commands if your setup allows it, or use the MTProto API with frameworks such as Pyrogram or Telethon.
I want to code a timed ban or mute, but with that I can restart my bot. Is there a nice libary or anyone has an idea to code it?
Thank you very much!
I code with discordpy cogs
You would have to use a database system such as MongoDB or MySQL. You will have to store the active timed ban/mute in a record/document or another table/collection. Then if you are using MySQL you would only select user's that are timed by using Tasks. In MongoDB, you just search for any users that are timed.
You would either make a separate cog or keep it in your moderation cog, it must be executed when it's ready and execute every 5-30 minutes checking if the time is older than the current time. You could use timestamps to accomplish this. Then just update the document/record once the time is up and remove their id from a ban list.
If it involves restarting your bot, then you cannot use your RAM to store your data but you need to use your Hard Disk for that. When your bot is running, it is storing its data inside the RAM and that's why you can re-use them while the bot is online. Once it goes offline or gets restarted, all the data are removed from the RAM because the program is shut down.
To store those data within the Hard Disk, you need a database. For such small projects, you can use JSON or SQLite. If the project scales, you can move to another SQL like MySQL that will handle a more complex and heavy database.
To make a bot that can do a timed message:
You need to store the data of when the message is going to be sent on your hard disk (database), then use that data to send that message. For example, you want to send "hello" in 1 day. That basically means that you want to send it at 8/7/2021 6:19 PM (it's 7/7/2021 6:19 PM right now). So, you store 8/7/2021 6:19 PM as a piece of data of when the bot is going to send the message.
Then you make the bot compare the current time with the time that you saved on your database. If it is greater, then it will send the message and delete the data from the database.
You can use the same technique with timed ban, role and everything else.
From a technical standpoint, you can use Discordpy for all the Discord stuff, datetime to check the time, JSON (or SQlite3) for the database.
i have a telegram -bot in a group which sends messages like current weather, stock prices , news and so on. at the end of the day i want to clean up the messages sent by bot. however i unable to get the message id of the messages sent by bot using the api 'https://api.telegram.org/botxxxx:xxxxxxxxxxx/getUpdates' and hence i am unable to delete message created by bot. any updates on this issue will be appreciated
you should store the message IDs when invoking the send method (from the message object returned) and delete those messages while emptying out your message id storage later on.
This is efficient than your approach for couple of reasons. For instance, with your approach you may have to filter out other messages/updates on that channel that weren't send by the bot (if your bot isn't the only admin who posts content).