Advertisement
Guest User

functions.py

a guest
Nov 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. from random import *
  2. import requests
  3. import pycountry
  4. import pytz
  5. from google import google
  6. import smtplib
  7.  
  8. greetings = ["hello", "hi", "hey", "yo"]
  9. farewell = ["goodbye", "cya later", "bye", "cya", "later"]
  10. browser = ["google", "search"]
  11. clock = ["time", "hours", "clock","timezone"]
  12. rollDice = ["roll", "dice", "die"]
  13. weatherList = ["weather", "hot", "temperature"]
  14. emailList = ["email", "send"]
  15.  
  16.  
  17. def greet(user):
  18. randomGreet = choice(greetings).capitalize()
  19. return randomGreet
  20.  
  21. def endFunction(user):
  22. randomBye = choice(farewell).capitalize()
  23. return randomBye
  24.  
  25. def googleSearch(custom, after):
  26. if after == False:
  27. term = input(" Bot : What would you like to search?\n User: ")
  28. else:
  29. term = custom
  30. search_results = google.search(term)
  31. print(" Bot: Here is what I found for '" + term + "':")
  32. for result in search_results:
  33. if result.index == 5:
  34. break
  35. else:
  36. result.name = result.name.replace(result.link,"")
  37. print(" " + str(result.index+1) + ": " + result.name + " - " + result.link + "\n")
  38.  
  39.  
  40.  
  41. def weather():
  42.  
  43. city = input("Enter the name of your city : ")
  44.  
  45. api_address = 'https://api.openweathermap.org/data/2.5/weather?q={}&appid=a283f94f4370e92593315243cecd791b'.format(city)
  46. url = api_address
  47. json_data = requests.get(url).json()
  48. formatted_data = json_data['weather'][0]['description']
  49. temp = json_data['main']['temp']
  50. tempc = temp -273.15
  51. print(' Bot: Today is expected in '+ city + ' ' + formatted_data)
  52. print(' Bot: The temperature is ',round(tempc,2),'°C')
  53. return tempc
  54.  
  55. def checkTimeZone():
  56. term = input(" Bot: Enter a country name and I'll tell you the time in that country\n User: ")
  57. print(" Bot: Please wait...")
  58. try:
  59. countinfo = pycountry.countries.get(name=term.title())
  60. countcode = countinfo.alpha_2
  61. countzone = pytz.country_timezones(countcode)
  62. except:
  63. print(" Bot: Sorry, I don't recognise the name of that country")
  64. else:
  65. timereq = requests.get("http://api.timezonedb.com/v2.1/get-time-zone?key=A56Y2B25QKH8&format=json&by=zone&zone={}".format(countzone[0])).json()
  66. timegot = timereq["formatted"]
  67. print(" Bot: The current time in {a} is {b}".format(a=term.title(),b=timegot[11:16]))
  68. return term
  69.  
  70. def diceRoll(number):
  71. if number >= 1:
  72. roll = randint(1,number)
  73. else:
  74. sides = int(input(" Bot: How many sides does the die have?\n User: "))
  75. roll = randint(1,sides)
  76. print(" Bot: You rolled a " + str(roll) + "!")
  77. return number
  78.  
  79. def emailFunction():
  80. user_email = input(" Bot: Please enter the email address that you would like to send an email to: ")
  81.  
  82. print(" ")
  83.  
  84. subject= (input(" Bot: Enter the subject of the email: "))
  85.  
  86. print(" ")
  87.  
  88. body = (input(" Bot: Enter the content of the email: "))
  89.  
  90. print(" ")
  91.  
  92. aemail = input(" Bot: Would you like to send this email to anyone else?\n User: ")
  93.  
  94. x = 'Subject: {}\n\n{}'.format(subject, body)
  95.  
  96. if aemail == "yes" or aemail.upper() == "YES":
  97.  
  98. print(" ")
  99.  
  100. oemail= input("Write the other email --> ")
  101.  
  102. server = smtplib.SMTP('smtp.gmail.com', 587)
  103. server.starttls()
  104.  
  105. # chatbot email login
  106. server.login("chatbottestcu123@gmail.com", "chatbot123")
  107.  
  108. # code to send the email to the user
  109. server.sendmail("chatbottestcu123@gmail.com", user_email, x)
  110. server.sendmail("chatbottestcu123@gmail.com", oemail , x)
  111. server.quit()
  112.  
  113.  
  114. elif aemail.lower() == "no" or aemail.upper()== "N0" :
  115.  
  116. server = smtplib.SMTP('smtp.gmail.com', 587)
  117. server.starttls()
  118.  
  119. server.login("chatbottestmail2@gmail.com", "chatbottest123")
  120. server.sendmail("chatbottestcu123@gmail.com", user_email, x)
  121.  
  122. print(" Bot: Email sent!")
  123.  
  124. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement