GithubHelp home page GithubHelp logo

arshiaafr / send-message-pyrogram Goto Github PK

View Code? Open in Web Editor NEW

This project forked from viaxco/send-message-pyrogram

0.0 0.0 0.0 10 KB

Automatically send/schedule messages using the Pyrogram framework for Telegram

License: MIT License

Python 100.00%

send-message-pyrogram's Introduction

send-message-pyrogram

A python script used to automatically send/schedule messages to users periodically using the Pyrogram framework for Telegram, the schedule and time modules.

Installation

Simple installation

Deploy

Manual installation

You can clone the repo:

git clone https://github.com/ViaxCo/send-message-pyrogram.git

Install the dependencies:

pip install -r requirements.txt

On your local system, you can create a .env file to store your environment variables; API_ID, API_HASH and SESSION_STRING. These will then be loaded by the python-dotenv module.

To generate the session_string, run:

python3 GenerateSessionString.py

This will lead you to input your phone number and authenticate yourself with Telegram if you haven't already, and proceed to print your session string, which you should copy and place in the SESSION_STRING variable in your .env file.

How to use

You can run the program using:

python3 send-message-pyrogram.py

To change the receiver of the message, simply change the number to the id of the contact you wish to send the message to:

app.send_message(1234567890, "Your message")

To get the id of the contact you wish to send the message to, you can do this by getting all the contacts, printing them to a .json file and searching for the contact in the file by name and copying the id from there.

with app:
    f = open("contacts.json", "a")
    f.write(str(app.get_contacts()))
    f.close()

In your contacts.json file, a user would look like this:

{
  "_": "User",
  "id": 1234567890,
  "is_self": false,
  "is_contact": true,
  "is_mutual_contact": true,
  "is_deleted": false,
  "is_bot": false,
  "is_verified": false,
  "is_restricted": false,
  "is_scam": false,
  "is_support": false,
  "first_name": "Contact name",
  "status": "offline",
  "last_online_date": "2020-01-15 09:20:30",
  "dc_id": 4,
  "phone_number": "*************",
  "photo": {
    "_": "ChatPhoto",
    "small_file_id": "CQADBCADrKcxGyZjUyMACCkTqhsABAIAAyZjUyMABBQv95-EGuw8RNcCAAEWBD",
    "big_file_id": "CQADBCADrKcxGyZjUyMACCkTqhsABAMAAyZjUyMABBQv95-EGuw8RtcCAAEWBD"
  }
}

You can then copy the id of the contact and use it.

To change frequency of the messages

Using the time module

Example
#USING THE INBUILT TIME MODULE
with app:
    n = 0
    while n < 3:
        # Send message with 10 minutes interval
        app.send_message("me", "Your message")
        print("Message sent successfully!")
        print("Waiting...")
        time.sleep(600)
        n += 1

This sends the message 3 times with 10 minutes interval. The number of times the message should be sent can be changed by changing the number: n < 3. The time interval between messages can be changed by changing the number appropriately in seconds: time.sleep(600).

Using the schedule module

Example
# USING THE SCHEDULE MODULE
n = 0


def job():
    global n
    app.send_message("me", "Your message")
    print("Message sent successfully!")
    print("Waiting...")
    n += 1


# schedule.every().day.at("10:30").do(job)
# schedule.every(5).seconds.do(job)
schedule.every(30).minutes.do(job)
# schedule.every().hour.do(job)
# schedule.every().monday.do(job)
# schedule.every().wednesday.at("13:15").do(job)
# schedule.every().minute.at(":17").do(job)

with app:
    while True:
        schedule.run_pending()
        time.sleep(1)
        if n == 18:
            break

This sends a message 18 times with 30 minutes interval. The number of times the message should be sent can be changed by changing the number: if n == 18. The time interval between messages can be changed by changing the values of schedule.every()... as needed.

Notice about the schedule module

When deployed remotely on somewhere like Heroku, note that Heroku's servers use UTC+0, so every time value you explicitly set should be in UTC+0 to match your time zone. Also, for times with single digit hours such as "7:15", make sure to add a leading zero: "07:15".

Enjoy!

send-message-pyrogram's People

Contributors

viaxco avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.