• 18-12-2024, 17:01:20
    #1
    Merhaba arkadaşlar,

    Bir telegram botu oluşturdum. Python scripti hazırladım ama telegram üzerinden mesaj alamıyorum. Kodlarımın neresinde yanlışlık var, rica etsem bakabilir misiniz? Amacım belirtmiş olduğum txt dosyasında yer alan verileri belli aralıklarla kontrol ederek referans değer dışına çıkıldığında telegram üzerinden mesaj almak. Şimdiden teşekkürler.

    Token ve Chat ID'lerde problem yok.

    import csv
    import requests
    import time
    
    BOT_TOKEN = "TOKEN" 
    CHAT_ID = "-GRUP CHAT ID" 
    
    TEMP_MIN = 20.0
    TEMP_MAX = 25.9
    HUMIDITY_MIN = 40
    HUMIDITY_MAX = 60
    
    def send_telegram_message(message):
        url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
        data = {"chat_id": CHAT_ID, "text": message}
        try:
            response = requests.post(url, data=data)
            if response.status_code == 200:
                print("Telegram bildirimi gönderildi.")
            else:
                print(f"Telegram bildirimi gönderilemedi. Hata kodu: {response.status_code}")
        except Exception as e:
            print(f"Hata: {e}")
    
    def process_data(file_path, notified_entries):
        try:
            with open(file_path, "r", encoding="utf-8") as file:
                reader = csv.reader(file, delimiter=";")
                next(reader)
                
                for row in reader:
                    if len(row) < 3:
                        continue
                    
                    date = row[0].strip('"')
                    indoor_temp = row[1].strip('"')
                    indoor_humidity = row[2].strip('"')
                    
                    if indoor_temp == "---" or indoor_humidity == "---":
                        continue
                    
                    try:
                        temp = float(indoor_temp)
                        humidity = float(indoor_humidity)
                        
                        if temp < TEMP_MIN or temp > TEMP_MAX or humidity < HUMIDITY_MIN or humidity > HUMIDITY_MAX:
                            unique_key = f"{date}_{temp}_{humidity}"
                            if unique_key not in notified_entries:
                                message = (f"Dikkat! {date} tarihinde ölçülen değerler referans aralığın dışında:\n"
                                           f"Sıcaklık: {temp}°C (Aralık: {TEMP_MIN}-{TEMP_MAX}°C)\n"
                                           f"Nem: {humidity}% (Aralık: {HUMIDITY_MIN}-{HUMIDITY_MAX}%)")
                                send_telegram_message(message)
                                notified_entries.add(unique_key)  
                    except ValueError:
    
                        continue
        except FileNotFoundError:
            print(f"Hata: {file_path} dosyası bulunamadı!")
    
    if __name__ == "__main__":
        file_path = r"C:\Konum\data.txt" 
        notified_entries = set()
        print("Sistem çalışmaya başladı. Veriler yarım saatte bir kontrol ediliyor...")
        
        while True:
            process_data(file_path, notified_entries) 
            time.sleep(1800)
    Cihaz Datalogger çıktım aşağıdakine benzer bana sasdece ilk 3 kolondaki veriler gerekiyor.

    Örnek Çıktı:

    "Date;""Indoor Temperature"";""Indoor Humidity"";""Outdoor Temperature 1"";""Outdoor Humidity 1"";""Outdoor Temperature 2"";""Outdoor Humidity 2"";""Outdoor Temperature 3"";""Outdoor Humidity 3"";""Outdoor Temperature 4"";""Outdoor Humidity 4"";""Outdoor Temperature 5"";""Outdoor Humidity 5"""
    "02.08.2021 03:30;""23.7"";""57"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"""
    "02.08.2021 04:00;""23.7"";""57"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"""
    "02.08.2021 04:30;""23.7"";""57"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"""
    "02.08.2021 05:00;""23.7"";""57"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"""
    "02.08.2021 05:30;""23.7"";""57"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"""
    "02.08.2021 06:00;""23.8"";""57"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"";""---"""
  • 18-12-2024, 17:04:23
    #2
    Merhaba, bazı işaretçiler Python'da sorun oluşturabiliyor. Bir de şu kodu dener misin?


    import csv
    import requests
    import time
    
    BOT_TOKEN = "TOKEN"
    CHAT_ID = "-GRUP CHAT ID"
    
    TEMP_MIN = 20.0
    TEMP_MAX = 25.9
    HUMIDITY_MIN = 40
    HUMIDITY_MAX = 60
    
    def send_telegram_message(message):
        url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
        data = {"chat_id": CHAT_ID, "text": message}
        try:
            response = requests.post(url, data=data)
            if response.status_code == 200:
                print("Telegram bildirimi gönderildi.")
            else:
                print(f"Telegram bildirimi gönderilemedi. Hata kodu: {response.status_code}")
        except Exception as e:
            print(f"Telegram gönderim hatası: {e}")
    
    def process_data(file_path, notified_entries):
        try:
            with open(file_path, "r", encoding="utf-8") as file:
                reader = csv.reader(file, delimiter=";", quotechar='"')
                next(reader)  # Başlık satırını atla
                
                for row in reader:
                    if len(row) < 3:
                        continue
                    
                    date = row[0].strip('"')
                    indoor_temp = row[1].strip('"')
                    indoor_humidity = row[2].strip('"')
    
                    if indoor_temp == "---" or indoor_humidity == "---":
                        continue
                    
                    try:
                        temp = float(indoor_temp)
                        humidity = float(indoor_humidity)
                        
                        if temp < TEMP_MIN or temp > TEMP_MAX or humidity < HUMIDITY_MIN or humidity > HUMIDITY_MAX:
                            unique_key = f"{date}_{temp}_{humidity}"
                            if unique_key not in notified_entries:
                                message = (f"Dikkat! {date} tarihinde ölçülen değerler referans aralığın dışında:\n"
                                           f"Sıcaklık: {temp}°C (Aralık: {TEMP_MIN}-{TEMP_MAX}°C)\n"
                                           f"Nem: {humidity}% (Aralık: {HUMIDITY_MIN}-{HUMIDITY_MAX}%)")
                                send_telegram_message(message)
                                notified_entries.add(unique_key)  
                    except ValueError as e:
                        print(f"Veri dönüştürme hatası: {indoor_temp}, {indoor_humidity}, Hata: {e}")
        except FileNotFoundError:
            print(f"Hata: {file_path} dosyası bulunamadı!")
        except Exception as e:
            print(f"Dosya işleme hatası: {e}")
    
    if __name__ == "__main__":
        file_path = r"C:\Konum\data.txt"
        notified_entries = set()
        print("Sistem çalışmaya başladı. Veriler yarım saatte bir kontrol ediliyor...")
        
        while True:
            process_data(file_path, notified_entries)
            time.sleep(1800)
  • 18-12-2024, 17:11:50
    #3
    JimmyBrown adlı üyeden alıntı: mesajı görüntüle
    Merhaba, bazı işaretçiler Python'da sorun oluşturabiliyor. Bir de şu kodu dener misin?


    import csv
    import requests
    import time
    
    BOT_TOKEN = "TOKEN"
    CHAT_ID = "-GRUP CHAT ID"
    
    TEMP_MIN = 20.0
    TEMP_MAX = 25.9
    HUMIDITY_MIN = 40
    HUMIDITY_MAX = 60
    
    def send_telegram_message(message):
        url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
        data = {"chat_id": CHAT_ID, "text": message}
        try:
            response = requests.post(url, data=data)
            if response.status_code == 200:
                print("Telegram bildirimi gönderildi.")
            else:
                print(f"Telegram bildirimi gönderilemedi. Hata kodu: {response.status_code}")
        except Exception as e:
            print(f"Telegram gönderim hatası: {e}")
    
    def process_data(file_path, notified_entries):
        try:
            with open(file_path, "r", encoding="utf-8") as file:
                reader = csv.reader(file, delimiter=";", quotechar='"')
                next(reader)  # Başlık satırını atla
                
                for row in reader:
                    if len(row) < 3:
                        continue
                    
                    date = row[0].strip('"')
                    indoor_temp = row[1].strip('"')
                    indoor_humidity = row[2].strip('"')
    
                    if indoor_temp == "---" or indoor_humidity == "---":
                        continue
                    
                    try:
                        temp = float(indoor_temp)
                        humidity = float(indoor_humidity)
                        
                        if temp < TEMP_MIN or temp > TEMP_MAX or humidity < HUMIDITY_MIN or humidity > HUMIDITY_MAX:
                            unique_key = f"{date}_{temp}_{humidity}"
                            if unique_key not in notified_entries:
                                message = (f"Dikkat! {date} tarihinde ölçülen değerler referans aralığın dışında:\n"
                                           f"Sıcaklık: {temp}°C (Aralık: {TEMP_MIN}-{TEMP_MAX}°C)\n"
                                           f"Nem: {humidity}% (Aralık: {HUMIDITY_MIN}-{HUMIDITY_MAX}%)")
                                send_telegram_message(message)
                                notified_entries.add(unique_key)  
                    except ValueError as e:
                        print(f"Veri dönüştürme hatası: {indoor_temp}, {indoor_humidity}, Hata: {e}")
        except FileNotFoundError:
            print(f"Hata: {file_path} dosyası bulunamadı!")
        except Exception as e:
            print(f"Dosya işleme hatası: {e}")
    
    if __name__ == "__main__":
        file_path = r"C:\Konum\data.txt"
        notified_entries = set()
        print("Sistem çalışmaya başladı. Veriler yarım saatte bir kontrol ediliyor...")
        
        while True:
            process_data(file_path, notified_entries)
            time.sleep(1800)
    Cevabınız için teşekkür ederim fakat yine bir mesaj almadım. Datalogger işaretçileriyle ilgili mi bir sorun var anlayamadım
  • 18-12-2024, 17:19:59
    #4
    sorunu çözdüm arkadaşlar teşekkürler