· 6 years ago · Mar 25, 2020, 09:58 AM
1#! /usr/bin/env python
2# -*- coding: utf-8 -*-
3import vk_api
4from vk_api.bot_longpoll import VkBotLongPoll
5from vk_api.utils import get_random_id
6from datetime import datetime, timezone
7import time
8import random
9vk = vk_api.VkApi(token='3709764b230f73dbf6a6fca9c41e726af8c0e28eff5ea171f2e25d6b5d3761711e9cf46b77cbf3d7c47ba') #токен паблика
10
11class MyVkLongPoll(VkBotLongPoll): # создаю класс чтобы он не терял соединение с сервером вк когда тот перезагружается (перезагружается раз в день где-то глубоко ночью)
12 def listen(self):
13 while True:
14 try:
15 for event in self.check():
16 yield event
17 except Exception as e:
18 print(e) #будет писать в консоль что таймаут произошел, при желании просто впишите сюда pass
19
20bot_id = '193179464' # сюда id паблика
21longpoll = MyVkLongPoll(vk, bot_id)
22
23def send_message(msg):
24 vk.method('messages.send',{'random_id':get_random_id(),'peer_id':event.obj.peer_id,'message':msg})
25def weather():
26 # Python program to find current
27 # using openweathermap api
28
29 # import required modules
30 if 'рандом' in event.obj.text.lower():
31 spis1 = event.obj.text.split(' ')[1:]
32 if len(spis)>1:
33 send_message('Неправильно набрана команда!')
34 continue
35 else:
36 try:
37 import requests, json
38
39 # Enter your API key here
40 api_key = "c012abf83e8afdf40810863f64ae8618"
41
42 # base_url variable to store url
43 base_url = "http://api.openweathermap.org/data/2.5/weather?"
44
45 # Give city name
46 send_message("в каком городе вы живете?")
47 city_name = event.obj.text.lower()
48
49 # complete_url variable to store
50 # complete url address
51 complete_url = base_url + "appid=" + api_key + "&q=" + city_name
52
53 # get method of requests module
54 # return response object
55 response = requests.get(complete_url)
56
57 # json method of response object
58 # convert json format data into
59 # python format data
60 x = response.json()
61
62 # Now x contains list of nested dictionaries
63 # Check the value of "cod" key is equal to
64 # "404", means city is found otherwise,
65 # city is not found
66 if x["cod"] != "404":
67
68 # store the value of "main"
69 # key in variable y
70 y = x["main"]
71
72 # store the value corresponding
73 # to the "temp" key of y
74 current_temperature = y["temp"]
75
76 # store the value corresponding
77 # to the "pressure" key of y
78 current_pressure = y["pressure"]
79
80 # store the value corresponding
81 # to the "humidity" key of y
82 current_humidiy = y["humidity"]
83
84 # store the value of "weather"
85 # key in variable z
86 z = x["weather"]
87
88 # store the value corresponding
89 # to the "description" key at
90 # the 0th index of z
91 weather_description = z[0]["description"]
92
93 # print following values
94 send_message(" Temperature (in kelvin unit) = " +
95 str(current_temperature) +
96 "\n atmospheric pressure (in hPa unit) = " +
97 str(current_pressure) +
98 "\n humidity (in percentage) = " +
99 str(current_humidiy) +
100 "\n description = " +
101 str(weather_description))
102
103 else:
104 send_message(" City Not Found ")