Tkd-Alex / Telegram-InstaPy-Scheduling

A Telegram bot for scheduling InstaPy
152 stars 38 forks source link

[Windows] Take a Screenshot and send it to telegram! #22

Closed HCWcoder closed 5 years ago

HCWcoder commented 5 years ago

This is a option i needed to check my CMD logs of Main.py while runing :

Add this Function to Main.py

import requests
def scrn(bot, update):

    update.message.reply_text('Taking the screenshoot')

    img=ImageGrab.grab()
    img.save('screenshot.png')

    url = "https://api.telegram.org/bot"+telegram_token+"/sendPhoto";
    files = {'photo': open('screenshot.png', 'rb')}
    data = {'chat_id' : allowed_id[0]}

    r= requests.post(url, files=files, data=data)

then add this handler code to the bottom next to the others :

dp.add_handler(CommandHandler("scrn", scrn))

In your telegram send /scrn to get it,

P.S: pip install requests or pip3 install requests If missing

HCWcoder commented 5 years ago

@Tkd-Alex Im sure you can make it work with your code with a better lines of code 🍡 :D

Thanks alot

Tkd-Alex commented 5 years ago

I don't understand why this command is helpful. You want the screen of main,py logs right?

I suggest to create a command like this: /logs 10

Where the args (ex: 10) are the lines of logs to send in answer after the command. What do you think about? It's very easy to develop

HCWcoder commented 5 years ago

@Tkd-Alex

okey i made this so far can you help ?

if i run the python code alone i can get the lines output printed in CMD but its not sending them in the telegram app.

def log(bot, update, args):
    if str(update.message.chat_id) in allowed_id:
        try:
            update.message.reply_text("in try")
            usernames = [ a['username'].lower() for a in users ]
            if not args[0].lower() in usernames:
                update.message.reply_text("Sorry, username <b>{}</b> is not saved.".format(args[0]), parse_mode='HTML')
                return
            lines = args[1]
            f = 'C:/Users/xxx/Documents/GitHub/InstaPy/logs/xxx/general.log'
            with open(f, "r") as text_file:
                contents = text_file.readlines()[-lines:]
                for line in contents:
                    print(line)
                    update.message.reply_text(line, parse_mode='HTML')

        except (IndexError, ValueError):
            update.message.reply_text('Usage: /now <username> <numberoflines>')

dp.add_handler(CommandHandler("log", log, pass_args=True))

in CMD working :

C:\Users\xxx\Documents\GitHub\InstaPy>python xxx.py
INFO [2018-12-17 00:25:16] [xxx]  Inappropriate: 0

INFO [2018-12-17 00:25:16] [xxx]  Not valid users: 1

INFO [2018-12-17 00:32:26] [xxx]  Session started!

INFO [2018-12-17 00:32:36] [xxx]  Logged in successfully!

INFO [2018-12-17 00:32:41] [xxx]  User 'xxxxz' [1/3]

INFO [2018-12-17 00:33:13] [xxx]  Session started!

INFO [2018-12-17 00:33:24] [xxx]  Logged in successfully!

INFO [2018-12-17 00:34:57] [xxx]  Session started!

INFO [2018-12-17 00:35:07] [xxx]  Logged in successfully!

in xxx.py :

def log():
    f = 'C:/Users/xxx/Documents/GitHub/InstaPy/logs/xxx/general.log'
    with open(f, "r") as text_file:
        contents = text_file.readlines()[-10:]
        for line in contents:
            print(line)
log()
Tkd-Alex commented 5 years ago

It should be work. I can't help you atm, As soon as I have free time I'll implement this feature for you :)

HCWcoder commented 5 years ago

It should be work. I can't help you atm, As soon as I have free time I'll implement this feature for you :)

I think theres an issue in here only update.message.reply_text(line, parse_mode='HTML')

if you can just help me in this maybe line need to be passed to telegram in a different way

Edit:

I made some changes to test : added this function to send :

def sends(msg2, chat_id2= chat_id, token1=my_token2):
    bot = telegram.Bot(token=token1)
    bot.sendMessage(chat_id=chat_id2, text=msg2)
def log(bot, update, args):
    if str(update.message.chat_id) in allowed_id:
        try:
            usernames = [ a['username'].lower() for a in users ]
            if not args[0].lower() in usernames:
                update.message.reply_text("Sorry, username <b>{}</b> is not saved.".format(args[0]), parse_mode='HTML')
                return

            #working on the logs and sending them to TELEGRAM

            lines = args[1]
            f = 'C:/Users/xxx/Documents/GitHub/InstaPy/logs/xxx/general.log'
            with open(f, "r") as text_file:
                contents = text_file.readlines()[-lines:]
                sends(contents)
            #-----------------------------------------------------------

        except BadRequest:
            update.message.reply_text('issue here')
        except (IndexError, ValueError):
            update.message.reply_text('Usage: /now <username> <numberoflines>')
    else:
        message = 'You have not the permission to use this bot.\nFor more details visit [Telegram-InstaPy-Scheduling](https://github.com/Tkd-Alex/Telegram-InstaPy-Scheduling)'
        update.message.reply_text(message, parse_mode='Markdown')

And it worked but look the results : ["INFO [2018-12-17 00:25:16] [xxx] Inappropriate: 0\n", "INFO [2018-12-17 00:25:16] [xxx] Not valid users: 1\n", "\n", "INFO [2018-12-17 00:32:26] [xxx] Session started!\n", "INFO [2018-12-17 00:32:36] [xxx] Logged in successfully!\n", "INFO [2018-12-17 00:32:41] [xxx] User 'xxx' [1/3]\n", "INFO [2018-12-17 00:33:13] [xxx] Session started!\n", "INFO [2018-12-17 00:33:24] [xxx] Logged in successfully!\n", "INFO [2018-12-17 00:34:57] [xxx] Session started!\n", "INFO [2018-12-17 00:35:07] [xxx] Logged in successfully!\n"]

how can i make it send without the brackets and line by line ?