-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_agent.py
More file actions
51 lines (48 loc) · 2.13 KB
/
run_agent.py
File metadata and controls
51 lines (48 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
import vk_api
import vk_api
import random
import time
import getpass
####Variables############################
access_token_app = "" #Application token
access_token_user = "" #Fake group here
group_hse_id = -1 #Id of group with which we work
user_vk_id = 1 #Real user id, need to send notifications
comment_message = "" #Here you write the comment
####Code#################################
print("VK Volleyball initial")
vk = vk_api.VkApi(token=access_token_app, scope=4096+8192)
vk_user = vk_api.VkApi(token=access_token_user, scope=4096+8192)
api = vk.get_api()
api_user = vk_user.get_api()
start_script_execution=1
latest_record = 0
iter_num = 0
###Fetch
while (True):
iter_num = iter_num + 1
print("Fetch latest record from public")
latest_record_tmp = api.wall.get(owner_id=group_hse_id, count=1)
print("Message: "+str(latest_record_tmp["items"][0]["id"])+" >>> "+latest_record_tmp["items"][0]["text"])
#If latest message is new
if (latest_record_tmp["items"][0]["id"]!=latest_record):
print("Got new post!")
latest_record = latest_record_tmp["items"][0]["id"] # get it`s id
#If it is the first iteration, just rememder first id
if start_script_execution==1:
print("This is a start of the work of application")
start_script_execution = 0
else:
#Leave comment
print("Leave comment")
api_user.wall.createComment(owner_id=group_hse_id, post_id=latest_record, message=comment_message)
date_event = latest_record_tmp["items"][0]["text"].split("\n")[0]
private_message = "ВНИМАНИЕ!!! Вы были записаны на волейбол на {} Проверьте комментарий во избежание черезвычайной ситуации".format(date_event)
print("Send message")
#Send message
api_user.messages.send(user_id=user_vk_id, random_id=random.randint(0,100), message=private_message)
if iter_num%10==0:
#[SERVICE]Check every 10 iters that all works
api_user.messages.send(user_id=user_vk_id, random_id=random.randint(0,100), message="Приложение работает стабильно")
time.sleep(10)