telethon events wont get specific user's events - python-3.x

So i've been working on this script for few days now and the script seems to run correctly without getting any errors but the issue is that my point of the script is getting specific user's status changes and instead of that the script prints me everytime ANYONE from my contants changes they're status inculding myself.
Please, does anyone think he can assist me ? I have been stuck on it for too long now and I am really desprate make that script works..
By the way, I was adding print(client.user_id) just to see if it works and I received the User ID of anyone in my contacts who made some kind of action.
from telethon.tl.types import UserStatusOffline
from telethon.sync import TelegramClient
from telethon import events
from datetime import datetime
import time
### Client Side ###
target = ""
phone = "+"
api_id =
api_hash = ""
client = TelegramClient(phone, api_id, api_hash)
client.start()
if client.is_user_authorized():
print(f"Session started at : {datetime.now()}")
time.sleep(2)
print("Logging in to Telegram complete.")
time.sleep(2)
else:
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
print(f"Started listening to {target}'s status...")
time.sleep(2)
target_id = client.get_peer_id(target)
print(f"{target}'s User ID is : {target_id}")
time.sleep(2)
############################################
# First status check to see rather #
# the user is correctly online or not #
# once it prints hes correct statement #
# global value will be changed so it wont #
# be printed again and again. #
############################################
first_msg = False
async def first_con():
if first_msg == False:
account = await client.get_entity(target)
if isinstance(account.status, UserStatusOffline):
print(f"{target} is correctly Offline.")
first_msg = True
else:
print(f"{target} is correctly Online.")
first_msg = True
else:
print("Something went wrong checking correct status.")
##################EVENTS####################
# Only events thats occurred after script #
# first run will pop up with prints ! #
# Every event that doesn't come from the #
# target gets as "event from username" #
############################################
#client.on(events.UserUpdate())
async def handler(event):
await first_con()
time.sleep(2)
if event.user_id == target_id:
if event.online:
print(f"{target} went Online at : {datetime.now()}")
elif event.recently:
print(f"{target} was recently online at : {datetime.now()}")
elif event.typing:
print(f"{target} typed a message at : {datetime.now()}")
else:
print("Sorry there was an error.")
else:
#print("Event from non-intersting user.") debugging propuses only
client.run_until_disconnected()

Let's zoom in on the code:
#client.on(events.UserUpdate())
async def handler(event):
x = await client.get_entity(target)
target_id = x.id
event.input_user = target_id
if event.input_user == target_id:
Let's tear it apart:
event.input_user = target_id
if event.input_user == target_id:
Let's give it different names. event.input_user will be foo and target_id will be bar:
foo = bar
if foo == bar:
You just assigned a value to the first variable, and are then comparing the first variable to that same value. Of course, this is always True, for any foo and bar values.
Modifying the event is generally a bad idea and I'm not sure what your intention was, but it seems to me that you should be caching the integer ID and then comparing that instead of making the call to get_entity every time (because input_user is a InputPeerUser and won't compare with User, which is why I guess you tried the weird assign):
target_id = client.get_peer_id(target)
#client.on(events.UserUpdate)
async def handler(event):
if event.user_id == target_id:
...
This way only one call is made at most, and you compare integers which is fast. Make sure to check the documentation on UserUpdate for more information.

Related

passing one function to another inside a for loop

New to programming. I wrote a program that Asks the user to enter the name, age and shoe size for four people and adds it to a dictionary. In fact, it works even when I don't use one function as an argument for another function. However, when i try to pass get_user_info() to store_user_info it doesnt work. I also tried to pass get_user_info to three separate variables and then passed these variables to store_user_info and it still didn't work. I am probably making a dumb error. Sorry its kind of a basic type of query but I just started learning programming. Any guidance is appreciated.
FOLLOWING CODE DOESN'T WORK: IT RUNS THE FOR LOOP BUT NEVER PROMPTS FOR THE INPUT FOR MORE THAN ONCE
#get user info
def get_user_info():
while True:
try:
user_input_name = (input("What is your name "))
user_input_age = int(input("How old are you "))
user_input_shoesize = float(input("What is your show size "))
break
except (ValueError,IndexError):
print('wrong selection or input')
return user_input_name,user_input_age,user_input_shoesize
#Store user info
def store_user_info(user_info):
user_information = {}
for i in range(3):
name, age, shoesize = user_info
user_information[name] = {"age" : age,"shoesize": shoesize}
print(user_information)
return user_information
=
store_user_info(get_user_info())
YET THE FOLLOWING WORKS and the loop works 3 times as expected:
#get user info
def get_user_info():
while True:
try:
user_input_name = (input("What is your name "))
user_input_age = int(input("How old are you "))
user_input_shoesize = float(input("What is your show size "))
break
except (ValueError,IndexError):
print('wrong selection or input')
return user_input_name,user_input_age,user_input_shoesize
#Store user info
def store_user_info():
user_information = {}
for i in range(3):
name, age, shoesize = get_user_info()
user_information[name] = {"age" : age,"shoesize": shoesize}
print(user_information)
return user_information
store_user_info()

loop to check for issues

i have this small program to assign issues to me, it works great on pycharm but when i compile it to exe with pyintaller it closes after the first run, ignoring the time.sleep and running again creating a loop to check for issues every 5 seconds.
why is that? how can i fix it?
import time
import winsound
from jira import JIRA
global lst_ignore
issue_var = ""
lst_ignore = []
def jira_login():
global user,token
user = 'user#cloud.com'
token = 'kj432hj43214YMzCyMLOe7682'
try:
options = {'server': 'https://cloud.atlassian.net'}
global jira
jira = JIRA(options=options, basic_auth=(user, token))
except Exception as e:
jira = ""
if '401' in str(e):
print("Login to JIRA failed. Check your username and password",e)
return jira
def check_issue():
jira_login()
size = 100000
start = 0 * 100000
search_query = 'status not in (Done, Closed,Canceled) and assignee is EMPTY and project = "Success" and reporter not in (57f778:f48131cb-b67d-43c7-b30d-2b58d98bd077)'
issues = jira.search_issues(search_query, start, size)
for issue in issues:
if issue not in lst_ignore:
winsound.Beep(2000, 1000)
issue.update(assignee={'accountId': '60f1d072a0de61930ad83770'})
print(issue, " : assinged to you!")
lst_ignore.append(issue)
def check():
check_issue()
time.sleep(5)
check()
if __name__ == '__main__':
check()
NEW ANSWER:___________________________________________________
(New info obtained from comment) If it says you don't have the module when running from the console, but it does from Pycharm, you probably have 2 different Interpreters. There are two steps you must take.
Using the terminal run: pip install jira
If it says it is already installed, uninstall using pip, and then reinstall.
Good Luck!
OLD ANSWER:____________________________________________________
It is hard to say. One thing I can tell you is that a recursive function is almost never a good idea.
So instead of:
def check():
check_issue()
time.sleep(5)
check()
if __name__ == '__main__':
check()
Try the following aproach:
RUNNING = True
def main():
while RUNNING:
check_issue()
time.sleep(5)
if __name__ == '__main__':
main()
After that you can implement that if some condition is met, it wil change the RUNNING constant to exit the program. Or, if it is a console-run program, then just use a keyboard-interrupt

State name spellcheck code not working with two word state names

I am working on vetting someone else's state spellchecker. The test data they ran seemed to work fine, but trying a different data set, it doesn't seem to be able to get past the first word "North" in a state name.
I need the code to be able to work with state names with two words.
This is the code:
import sys
!pip install pyspellchecker
from spellchecker import SpellChecker
#from google.colab import files
import pandas as pd
import io
#Implement spellcheck.
spell=SpellChecker()
for ind in newDF.index:
stateWordList = newDF['State'][ind].split()
if len(stateWordList) == 1:
#print(True)
if stateWordList[0] in spell:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
else:
misspelledState = False in (stateWord in spell for stateWord in stateWordList)
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
Instead, it isn't seeing North WhateverState as valid, and returns:
'North' is not a valid state, please enter a correct spelling:
Does it need a condition specifically for two word names?
In your else statement, you have a logic error
else:
misspelledState = False in (stateWord in spell for stateWord in stateWordList)
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
Let's see misspelledState = False in (stateWord in spell for stateWord in stateWordList), if all the words in stateWordList is well spelled, you are checking with misspelledState = False in (True, True, ...), the result will be False.
Then go to the if-else condition, it will go to else condition where outputs the correction message:
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
You can use
misspelledState = all([stateWord in spell for stateWord in stateWordList])

cant get my while loops working the way i want it to

i am trying to get this code to work properly where if you input a number it will revert you back to the name=input prompt and once you enter alphabetical characters and not numerical characters it will allow you to move on to the next set of code but it keeps returning you to the name = input and doesnt let you through the rest of the code
def setup():
global name
global HP
global SP
global MP
while True:
try:
name = input('can you please tell me your name? ')
name2=int(name)
if not name.isalpha==True and not name2.isdigit==False:
break
except Exception:
print('please input your name')
continue
HP = randint(17,20)
SP = randint(17,20)
MP = randint(17,20)
print('welcome ['+name+':]: to the moon landing expedition')
There is a problem at name2=int(name) This causes an exception unless you type all numbers. In turn, this triggers the Exception and loops it forever. Your while loop seems fine.
What i think you should do:
while True:
name = input('What is your name')
isnum = False
for i in name:
if i.isnumeric():
isnum = True
break
if isnum:
print('Please type your name.')
continue
break

Thread Save Serial Connection in Telepot (Python)

I have a serial device (Arduino) regularly outputting log data, which shold be written in a Log file. Also the device takes spontaneous commands over serial. I send the commands to a Raspberry over Telegram, which are handled and sent to the arduino by Telepot, which runs in a separate thread.
How can I make sure that the two processes get along with each other?
I am a complete Beginner in Multithreading.
Here is a shortened version of my Code:
import time
import datetime
import telepot
import os
import serial
from time import sleep
ser = None
bot = None
def log(data):
with open('logfile', 'w') as f:
file.write("Timestamp" + data)
#The handle Function is called by the telepot thread,
#whenever a message is received from Telegram
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print( 'Command Received: %s' % command)
if command = '/start':
bot.sendMessage(chat_id, 'welcome')
elif command == 'close_door':
#This serial write could possibly happen while a
#ser.readline() is executed, which would crash my program.
ser.write("Close Door")
elif command == 'LOG':
#Here i should make sure that nothing
#is waiting from the Arduino
#so that the next two Serial lines are the Arduinos
#respoonce to the "LOG" command.
#and that hanlde is the only
#function talking to the Serial port now.
ser.write("LOG")
response = ser.readline()
response += "\0000000A" + ser.readline()
#The Arduinos response is now saved as one string
#and sent to the User.
bot.sendMessage(chat_id, response)
print("Command Processed.")
bot = telepot.Bot('BOT TOKEN')
bot.message_loop(handle)
ser = serial.Serial("Arduino Serial Port", 9600)
print( 'I am listening ...')
while True:
#anything to make it not run at full speed (Recommendations welcome)
#The log updates are only once an hour.
sleep(10)
#here i need to make sure it does not collide with the other thread.
while ser.in_waiting > 0:
data = ser.readline()
log(data)
This code is not my actual code, but it should represent exactly what I'm trying to do.
My last resort would be to put the serial code in the threads loop function, But this would require me to change the libary which would be ugly.
I looked up some stuff about Queues in Asincio, and locking functions. However i don't really understand how to apply that. Also I don't use the async telepot.
After reading more on locking and threads, I found an answer with help of the links provided in this Question: Locking a method in Python?
It was often recommended to use Queues, however I don't know how.
My solution (code may have errors, but the principle works)
import time
import random
import datetime
import telepot
import os
import serial
from time import sleep
#we need to import the Lock from threading
from threading import Lock
ser = None
bot = None
def log(data):
with open('logfile', 'w') as f:
file.write("Timestamp" + data)
#create a lock:
ser_lock = Lock()
#The handle Function is called by the telepot thread,
#whenever a message is received from Telegram
def handle(msg):
#let the handle function use the same lock:
global ser_lock
chat_id = msg['chat']['id']
command = msg['text']
print( 'Command Received: %s' % command)
if command == '/start':
bot.sendMessage(chat_id, 'welcome')
elif command == 'close_door':
#This serial write could possibly happen while a
#ser.readline() is executed, which would crash my program.
with ser_lock:
ser.write("Close Door")
elif command == 'LOG':
#Here i should make sure that nothing
#is waiting from the Arduino
#so that the next two Serial lines are the Arduinos
#respoonce to the "LOG" command.
#and that hanlde is the only
#function talking to the Serial port now.
#the lock will only be open when no other thread is using the port.
#This thread will wait untill it's open.
with ser_lock:
while ser.in_waiting > 0:
data = ser.readline()
log(data)
#Should there be any old data, just write it to a file
#now i can safely execute serial writes and reads.
ser.write("LOG")
response = ser.readline()
response += "\0000000A" + ser.readline()
#The Arduinos response is now saved as one string
#and sent to the User.
bot.sendMessage(chat_id, response)
print("Command Processed.")
bot = telepot.Bot('BOT TOKEN')
bot.message_loop(handle)
ser = serial.Serial("Arduino Serial Port", 9600)
print( 'I am listening ...')
while True:
#anything to make it not run at full speed (Recommendations welcome)
#The log updates are only once a
sleep(10)
#here i need to make sure it does not collide with the other thread.
with ser_lock:
while ser.in_waiting > 0:
data = ser.readline()
log(data)

Resources