-1

I'm writing a script to sent proxies file from a directory to telegram channel. With the help of some good folks I was able to complete it almost

from telegram import Bot, InputMediaDocument

BOT_TOKEN = "xxx"
CHAT_ID = xxx


def main():
    bot = Bot(BOT_TOKEN)
    file_paths = (
        "proxy/proxies/http.txt",
        "proxy/proxies/socks4.txt",
        "proxy/proxies/socks5.txt"
    )
    path_http = "proxy/proxies/http.txt"
    with open(path_http,'rb') as file_http:
     http_count = len(file_http.readlines())
    file_http.close()

    path_socks4 = 'proxy/proxies/socks4.txt'
    with open(path_socks4,'rb') as file_socks4:
     socks4_count = len(file_socks4.readlines())
    file_socks4.close()

    path_socks5 = 'proxy/proxies/socks5.txt'
    with open(path_socks5,'rb') as file_socks5:
     socks5_count = len(file_socks5.readlines())
    file_socks5.close()

    text = f"Total HTTPs:{http_count}\nTotal SOCKS4:{socks4_count}\nTotal SOCKS5:{socks5_count}"

  media_group = list()
  for f in file_paths:
        with open(f, "rb") as fin:
         caption = text if f == "proxy/proxies/http.txt" else ''
        fin.seek(0)
        media_group.append(InputMediaDocument(fin, caption=caption))

  bot.send_media_group(CHAT_ID, media=media_group)


if __name__ == "__main__":
    main()


Issues occurred

media_group = list()
                    ^
IndentationError: unindent does not match any outer indentation level

what I'm trying is to send those 3 proxy files with a caption to the first proxy file

Ajesh
  • 43
  • 10
  • you are missing quotation marks in ``if f == proxy/proxies/http.txt``. ANd if you write that something fails you must read error messages you get. – Psytho Dec 21 '22 at 08:21

3 Answers3

1

There are a few issues with your code:

  1. The lack of proper indentation
  2. Improper closing of brackets

The SyntaxError that you're getting on the line path_http = 'proxy/proxies/http.txt' is because you didn't close the brackets from the previous line (the line where you initialised the variable file_paths).

I also foresee that you'll face issues when you try to open the file with the open function. You can look up some examples here: https://www.programiz.com/python-programming/file-operation

Lee Kai Xuan
  • 156
  • 7
1

with the help of some good folks I make it work, If there are any easy versions fell free to share

from telegram import Bot, InputMediaDocument

BOT_TOKEN = "xxxxx"
CHAT_ID = -1000000

def http():
    path_http = 'proxy/proxies/http.txt'
    with open(path_http, 'rb') as file_http:
        http_count = len(file_http.readlines())
    return http_count
def socks4():
    path_socks4 = 'proxy/proxies/socks4.txt'
    with open(path_socks4, 'rb') as file_socks4:
        socks4_count = len(file_socks4.readlines())
    return socks4_count
def socks5():
    path_socks5 = 'proxy/proxies/socks5.txt'
    with open(path_socks5, 'rb') as file_socks5:
        socks5_count = len(file_socks5.readlines())
    return socks5_count        
http_count = http()
socks4_count = socks4()
socks5_count = socks5()
text = f"Total HTTPs:{http_count}\nTotal SOCKS4:{socks4_count}\nTotal SOCKS5: {socks5_count}"

def main():
    bot = Bot(BOT_TOKEN)
    file_paths = (
        "proxy/proxies/http.txt",
        "proxy/proxies/socks4.txt",
        "proxy/proxies/socks5.txt"
    )
    media_group = list()
    for f in file_paths:
        with open(f, "rb") as fin:
           caption = text if f == "proxy/proxies/socks5.txt" else '' 
           fin.seek(0)
           media_group.append(InputMediaDocument(fin, caption=caption))
    bot.send_media_group(CHAT_ID, media=media_group)

if __name__ == "__main__":
    main()    
Ajesh
  • 43
  • 10
0
`file_paths = (
        "proxy/proxies/http.txt",
        "proxy/proxies/socks4.txt",
        "proxy/proxies/socks5.txt"
`

invalid syntax error probably because ) doesn't exist in the end.
try adding (" ") caption = text if f == "proxy/proxies/http.txt" else '' because it is a string.

KhafitBZ
  • 11
  • 1