mohammadKa143 commited on
Commit
9132eff
·
verified ·
1 Parent(s): 6c32517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -2
app.py CHANGED
@@ -6,11 +6,99 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  import os
8
  from Gradio_UI import GradioUI
 
9
  from duckduckgo_search import DDGS
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  @tool
15
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
16
  #Keep this format for the description / args / args description but feel free to modify the tool
@@ -95,7 +183,7 @@ with open("prompts.yaml", 'r') as stream:
95
 
96
  agent = CodeAgent(
97
  model=gemini_model,
98
- tools=[final_answer,get_current_time_in_timezone,search_duckduckgo], ## add your tools here (don't remove final answer)
99
  max_steps=6,
100
  verbosity_level=1,
101
  grammar=None,
 
6
  from tools.final_answer import FinalAnswerTool
7
  import os
8
  from Gradio_UI import GradioUI
9
+
10
  from duckduckgo_search import DDGS
11
 
12
+ import pywhatkit
13
+ import datetime
14
+ import time
15
+
16
+
17
+
18
+ @tool
19
+ def send_whatsapp_message(phone_number: str = '+963934324595', message: str, hour: int = None, minute: int = None):
20
+ """
21
+ Sends a WhatsApp message using pywhatkit by automating WhatsApp Web.
22
+
23
+ This function schedules a WhatsApp message to be sent at a specific time,
24
+ or approximately one minute from now if no time is specified. It works by
25
+ opening WhatsApp Web in your default browser and simulating the sending
26
+ process.
27
+
28
+ Args:
29
+ phone_number (str): The recipient's phone number.
30
+ MUST include the country code with a '+',
31
+ e.g., "+12345678901".
32
+ message (str): The text message you want to send.
33
+ hour (int, optional): The hour (in 24-hour format, 0-23) to send
34
+ the message. Defaults to None.
35
+ minute (int, optional): The minute (0-59) to send the message.
36
+ Defaults to None. If hour or minute is None,
37
+ the message will be scheduled for ~1 minute
38
+ from the current time.
39
+
40
+ Returns:
41
+ bool: True if the message scheduling was attempted successfully by
42
+ pywhatkit, False if an error occurred.
43
+
44
+ --- IMPORTANT NOTES ---
45
+ - You MUST be logged into WhatsApp Web on the browser pywhatkit opens.
46
+ - This function will open a new browser tab/window.
47
+ - Sending is NOT instant; it happens *at* or slightly *after* the scheduled time.
48
+ - This method relies on web automation and can be unreliable if WhatsApp Web changes.
49
+ - Heavy automation might violate WhatsApp's Terms of Service. Use responsibly.
50
+ """
51
+ try:
52
+ send_h, send_m = 0, 0
53
+
54
+ if hour is None or minute is None:
55
+ # If no specific time is given, calculate 1 minute from now
56
+ now = datetime.datetime.now()
57
+ send_h = now.hour
58
+ send_m = now.minute + 1
59
+
60
+ # Handle minute and hour overflow
61
+ if send_m >= 60:
62
+ send_m -= 60
63
+ send_h += 1
64
+ if send_h >= 24:
65
+ send_h = 0
66
+ print(f"No time specified. Scheduling for ~1 minute from now: {send_h:02d}:{send_m:02d}")
67
+ else:
68
+ # Use the provided time
69
+ send_h = hour
70
+ send_m = minute
71
+ print(f"Scheduling for specified time: {send_h:02d}:{send_m:02d}")
72
+
73
+ print(f"Attempting to schedule for: {phone_number}")
74
+ print(f"Message: {message}")
75
+
76
+ # Use pywhatkit to schedule the message
77
+ pywhatkit.sendwhatmsg(
78
+ phone_no=phone_number,
79
+ message=message,
80
+ time_hour=send_h,
81
+ time_min=send_m,
82
+ wait_time=15, # Seconds for WhatsApp Web to load & send before closing tab
83
+ tab_close=True, # Close the tab after sending
84
+ close_time=3 # Seconds to wait *after* sending before closing
85
+ )
86
+
87
+ print("pywhatkit has successfully initiated the scheduling process.")
88
+ return True
89
+
90
+ except Exception as e:
91
+ print(f"An error occurred while trying to send WhatsApp message: {e}")
92
+ print("Things to check:")
93
+ print(" - Is pywhatkit installed (`pip install pywhatkit`)?")
94
+ print(" - Are you logged into WhatsApp Web in your default browser?")
95
+ print(" - Is the phone number format correct (e.g., '+12345678901')?")
96
+ return False
97
+
98
+
99
+
100
 
101
 
 
102
  @tool
103
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
104
  #Keep this format for the description / args / args description but feel free to modify the tool
 
183
 
184
  agent = CodeAgent(
185
  model=gemini_model,
186
+ tools=[final_answer,get_current_time_in_timezone,search_duckduckgo,send_whatsapp_message], ## add your tools here (don't remove final answer)
187
  max_steps=6,
188
  verbosity_level=1,
189
  grammar=None,