Python多功能的智能个人助理

import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pyowm

# 初始化语音引擎
engine = pyttsx3.init()

# 初始化语音识别器
recognizer = sr.Recognizer()

# 设置邮件发送相关信息
email_address = "your_email@gmail.com"
email_password = "your_password"
smtp_server = "smtp.gmail.com"
smtp_port = 587

# 初始化天气API
owm_api_key = "your_owm_api_key"
owm = pyowm.OWM(owm_api_key)

def speak(text):
    engine.say(text)
    engine.runAndWait()

def listen():
    with sr.Microphone() as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
        try:
            user_input = recognizer.recognize_google(audio)
            return user_input.lower()
        except sr.UnknownValueError:
            return "Could not understand audio"
        except sr.RequestError as e:
            return "Could not request results; {0}".format(e)

def wish_me():
    hour = datetime.datetime.now().hour
    if 0 <= hour < 12:
        speak("Good morning!")
    elif 12 <= hour < 18:
        speak("Good afternoon!")
    else:
        speak("Good evening!")

    speak("How can I assist you today?")

def send_email(receiver_email, subject, body):
    try:
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()
        server.login(email_address, email_password)
        message = "Subject: {}\n\n{}".format(subject, body)
        server.sendmail(email_address, receiver_email, message)
        server.quit()
        speak("Email sent successfully.")
    except Exception as e:
        speak("Sorry, I am unable to send the email at the moment.")

def get_weather_forecast(city):
    try:
        observation = owm.weather_at_place(city)
        weather = observation.get_weather()
        status = weather.get_status()
        temperature = weather.get_temperature('celsius')['temp']
        speak("The weather in {} is currently {}. The temperature is {} degrees Celsius.".format(city, status, temperature))
    except Exception as e:
        speak("Sorry, I couldn't retrieve the weather information for {}".format(city))

def main():
    wish_me()
    while True:
        user_input = listen()
        if "wikipedia" in user_input:
            speak("Searching Wikipedia...")
            user_input = user_input.replace("wikipedia", "")
            result = wikipedia.summary(user_input, sentences=2)
            speak("According to Wikipedia, ")
            speak(result)
        elif "open youtube" in user_input:
            webbrowser.open("https://www.youtube.com")
        elif "open google" in user_input:
            webbrowser.open("https://www.google.com")
        elif "play music" in user_input:
            music_dir = "C:\\Users\\User\\Music"
            songs = os.listdir(music_dir)
            if songs:
                os.startfile(os.path.join(music_dir, songs[0]))
        elif "send email" in user_input:
            speak("Who is the recipient?")
            recipient = listen()
            speak("What is the subject of the email?")
            subject = listen()
            speak("What should I say in the email?")
            body = listen()
            send_email(recipient, subject, body)
        elif "weather forecast" in user_input:
            speak("Which city's weather forecast would you like to know?")
            city = listen()
            get_weather_forecast(city)
        elif "exit" in user_input:
            speak("Goodbye!")
            break
        else:
            speak("I'm sorry, I didn't understand that.")

if __name__ == "__main__":
    main()
 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值