Alarm in pyTelegram Bot - python

I want to write a simple reminder-bot for telegram. Bot gets from user time (Hours:Minutes) and saves it. When system time equals to users remind time, bot sends message to user.
This is how i track current time:
import time
def timer():
now = time.strftime('%X').split(':')[0:2]
return now
The question is:
How can I make my code wait till time to send message comes without using time.sleep() and checking current time each minute (uses too much memory of raspberrypi)?

If you use python-telegram-bot library, you can use the JobQueue to set up timed jobs.
See here for an example.

Related

TeleBot, How to make bot auto reply message after 10mins

TeleBot, how to set it schedule send after user send message to our bot, i want it auto reply to user after 10mins
If by TeleBot you mean a Telegram-Bot then you have two options:
a) put in a CRON-Job that triggers a response function after given time
or
b) If it is okay that the process is inactive/unavailable for 10 mins then you can just let the programm sleep for 10 minutes (which is probably not what you want by using a chatbot because this would make the bot miss messages in that time if those are not buffered anywhere).
If neither fits your problem, please give us more information on the problem

Python asyncio bot with MongoDB

My experience in developing bots is very small and I recently had some problems... I need your help, options. This would be a great experience for me.
I need to make an asyncio Bot with automatic points for users (about 20000 users) every 10 minutes without lags and stopping the bot. My solution is to use Thread (library threading), but it takes a long time (about 20 minutes).
Question: Is there a better solution than this, since I'm pretty sure it's not the most efficient way to solve my problem?
while True:
# Thread for function to accrual points
pay_users = threading.Thread(
target=pay_function, args=[state, money])
# Thread start
pay_users.start()
while True:
# Check the end of thread
if pay_users.is_alive() is True:
await asyncio.sleep(5)
else:
# If Thread has been completed - join this Thread
pay_users.join()
break
# Every 10 minutes
await asyncio.sleep(600)
By the way, there is 1 other question that worries me. Let's say each user has a certain bonus that can be used(activated) every 24 hours. After the user has taken the bonus, he can click on the button and check how much time is left to restore the bonus. I want to make it so that when the user's ability to activate the bonus is restored, he will be informed about it. I thought about it and I came up with a trivial solution - at the moment of clicking on the bonus change the date field in the database to the date of the click, but I don't know how to make a notification with this solution about restoring the bonus.
Question: Is there any way to make an individual counter for each user, at the end of which to send a message to the function to notify the user about it?
Thank you very much in advance for your attention and trying to help me!

Loop reddit bot to check for answers every 10 minutes

I'm making a Reddit bot that goes through comments on certain subreddits and replies to those with certain keyphrases.
I originally did not have a loop, and it worked fine, but I had to click run again every few minutes. I am running my python script on pythonanywhere.com, using PRAW.
import praw
import time
SECONDS_PER_MIN = 60
subreddit = reddit.subreddit('memes+dankmemes+comics+funny+pics')
keyphrase = ('Sauce+Sauce?')
def main():
while True:
for comment in subreddit.stream.comments():
if keyphrase in comment.body:
comment.reply('[Here.](https://www.youtube.com/watch?v=dQw4w9WgXcQ)\n\nI am a bot and this action was performed automatically. Learn more at [https://saucebot.com/](https://www.youtube.com/watch?v=dQw4w9WgXcQ)')
print('Posted!')
time.sleep(SECONDS_PER_MIN * 11)
if __name__ == '__main__':
main()
I expect it to respond to a random person who says "sauce" every 10 minutes, but now it won't respond to anyone.
Are you running your script on a PC? You could potentially use the task scheduler for that without using python at all. Just save your script as a binary using pyinstaller, then schedule it to run every ten minutes.

How to add a pause between sending messages in python telegram bot?

I'm making a bot with python-telegram-bot that sends you several messages in a row in response to a single command. When all messages arrive at once, it is inconvenient to the user. I want to add a pause between sending and send action=ChatAction.TYPING between them. Is there any convenient way to do this without using something like time.sleep()?
I believe that the framework's JobQueue solves your problem. It allows you to schedule messages to be sent at some point in the future.
Quote:
You can also add a job that will be executed only once, with a delay:
>>> def callback_30(bot, job):
... bot.send_message(chat_id='#examplechannel',
... text='A single message with 30s delay')
...
>>> j.run_once(callback_30, 30)
In thirty seconds you should receive the message from callback_30.

My Jabber bot gets stuck -- how to throttle message sending?

I have my own jabber bot, and today I made a new plugin, which is to send message for all users. My code was working well, but I have a small problem; when I give my bot the command to send a message, my bot gets stuck and disconnects.
I know why my bot gets stuck and disconnects; I have more than 2000 users, so my bot cannot send a message at the same time for all users. Is there any method in Python to make my code send the message for each user after N seconds? I mean have the bot send MSG for user1, then wait for N seconds and send for user2, etc.
I hope my idea is clear. This is my code:
def send_msg(type, source, parameters):
ADMINFILE = 'modules/xmpp/users.cfg'
fp = open(ADMINFILE, 'r')
users = eval(fp.read())
if parameters:
for z in users:
msg(z, u"MSG from Admin:\n" +parameters)
reply(type, source, u"MSG has been sent!")
else:
reply(type, source, u"Error! please try again.")
register_command_handler(send_msg, 'msg', ['all','amsg'], 0,'Sends a message to all users')
I believe you are looking for time.sleep(secs). From the docs:
Suspend execution for the given number of seconds. The argument may be
a floating point number to indicate a more precise sleep time. The
actual suspension time may be less than that requested because any
caught signal will terminate the sleep() following execution of that
signal’s catching routine. Also, the suspension time may be longer
than requested by an arbitrary amount because of the scheduling of
other activity in the system.
After each send you can delay for time.sleep(seconds) before sending your next message.

Categories