Python Forum
send repeated messages with apscheduler
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
send repeated messages with apscheduler
#1
Hey there,

I've been working on my code, a bit, and i want to make it work more efficient.

I want to make a program that sends a random photo from a certain folder to a certain user every X hours/days.

User ids, folders and frequency may vary. I want to make a scalable solution to make it easy to add new users.

I want to make a function that can be called easily which has all the arguments (folder_name, user_id, frequency). I know the attached code is not working properly, but this is where i got stuck.

Help is much appreciated.

import fbchat
from fbchat.models import *
import random,os
from apscheduler.schedulers.blocking import BlockingScheduler
import docs
import lista_prieteni

client = fbchat.Client(docs.username, docs.password)
friend = client.searchForUsers(lista_prieteni.lista)
id1 = friend[0].uid
path1 = r"D:\Downloads\poze test\masini"


def mesaj_fb(path,id):

  random_filename = random.choice([
     x for x in os.listdir(path)
     if os.path.isfile(os.path.join(path, x))])
  print(random_filename)
  client.sendLocalImage(os.path.join(path, random_filename),message=Message(text="Doza ta zilnica de..."),thread_id=id)


def main():

    sched = BlockingScheduler()
    sched.add_job(mesaj_fb(path1,id1), 'interval', minutes=10)
    sched.start()

main()
i would like to create a function that looks like :

mesaj_fb (folder_name, user_id, frequency) - and to be able to call it easily in the main function everytime i want to add a new user. I don't want to add 10 new lines of code everytime i add a new user.
Reply
#2
When you use fbchat i don't think is running a loop(maybe in bots mode) and just do API call to the chat.
Then you need to make a running loop.
Use BackgroundScheduler so it don't block fbchat calls and your function call is wrong.
Here is a working example.
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import time, random

def mesaj_fb(path,my_id):
    print(f'Tick! The time is: {datetime.now()} {random.choice(path), my_id}')

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_job(mesaj_fb, 'interval', args=[('image_1.png','image_2.png','image_3.png'), 10], seconds=5)
    scheduler.start()
    print('Press Ctrl+C to exit')
    try:
        while True:
            time.sleep(2)
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()
Output:
C:\code λ python tick.py Press Ctrl+C to exit Tick! The time is: 2020-01-04 21:35:11.823093 ('image_3.png', 10) Tick! The time is: 2020-01-04 21:35:16.823867 ('image_3.png', 10) Tick! The time is: 2020-01-04 21:35:21.823910 ('image_1.png', 10) Tick! The time is: 2020-01-04 21:35:26.824082 ('image_2.png', 10) Tick! The time is: 2020-01-04 21:35:31.824071 ('image_3.png', 10)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  User serial/pyserial to send messages to an arudino via terminal manually bkapadia 2 2,659 Mar-10-2021, 11:26 AM
Last Post: Larz60+
  How to tabulate correctly repeated blocks? Xiesxes 4 2,868 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  How can i use max_instances in apscheduler zebisnaga 0 2,530 Jul-21-2019, 11:17 PM
Last Post: zebisnaga
  logging messages ahead of print messages vindo 6 3,140 Jun-18-2019, 02:45 PM
Last Post: vindo
  Help with apscheduler brakow87 5 6,006 Oct-21-2018, 07:16 PM
Last Post: buran
  create list from repeated pattern in python Code4fun 2 3,394 Sep-25-2018, 07:09 PM
Last Post: woooee
  Parsing Text file having repeated value key pair using python manussnair 3 3,231 Aug-04-2018, 11:48 PM
Last Post: micseydel
  How to convert Schedule to APScheduler module? penguin9 2 3,727 May-03-2018, 12:44 PM
Last Post: penguin9

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020