Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2. #!/usr/bin/python
  3.  
  4. def ru(x): return x.decode('utf-8')
  5.  
  6. import dbus, vk_api
  7. from vk_api.longpoll import VkLongPoll, VkEventType
  8. from time import sleep#, time
  9. from sys import setrecursionlimit
  10. setrecursionlimit(9999999)
  11.  
  12.  
  13. # wall doc audio photo sticker money_request
  14. attachments={}
  15. attachments["wall"]="запись со стены"
  16. attachments["doc"]="документ"
  17. attachments["audio"]="аудио"
  18. attachments["photo"]="фото"
  19. attachments["sticker"]="стикер"
  20. attachments["money_request"]="запрос денег"
  21. attachments["geo_provider"]="карта"
  22. attachments["audiomsg"]="голосовое сообщение"
  23. attachments["animation"]="стикер"
  24. attachments["video"]="видео"
  25. #attachments[""]=""
  26.  
  27. bus = dbus.SessionBus()
  28. object = bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications')
  29. interface = dbus.Interface(object,'org.freedesktop.Notifications')
  30. #print(interface.GetCapabilities())
  31.  
  32. def send_notify(f_name, l_name, c_name, txt, attach):
  33.  
  34. if len(c_name) == 0:
  35. top_txt=f_name+": "+txt+attach
  36. large_txt=f_name+" "+l_name
  37. small_txt=txt+attach
  38.  
  39. else:
  40. top_txt=c_name+": "+txt+attach
  41. large_txt=c_name
  42. small_txt=f_name+": "+txt+attach
  43.  
  44. interface.Notify("Контактик",
  45. 0,
  46. "icon-m-notifications",
  47. large_txt,
  48. small_txt,
  49. dbus.Array(["default", ""]),
  50. dbus.Dictionary({"x-nemo-preview-body": top_txt,
  51. "category": "x-nemo.batterynotifier"},
  52. signature='sv'),
  53. 0)
  54.  
  55. sleep(0.01)
  56.  
  57. def send_notif1y(f_name, l_name, c_name, txt, attach):
  58. interface.Notify("VK",
  59. 0,
  60. "icon-m-notifications",
  61. "1верхняя строка уведомления",
  62. "1нижняя строка уведомления",
  63. dbus.Array(["default", ""]),
  64. dbus.Dictionary({"x-nemo-preview-body": "1текст вверху",
  65. "category": "x-nemo.batterynotifier"},
  66. signature='sv'),
  67. 0)
  68.  
  69.  
  70.  
  71. #send_notify("алежа", "маркел", "я всех заебал")
  72.  
  73. def response(txt, user, attach, chatid):
  74. global vk
  75. #print("response called")
  76. txt = txt.replace('"',
  77. '\"')
  78. if user[1]>0:
  79. c_name=''
  80. if user[0] == 'user':
  81. f_name, l_name = user_parse(user)
  82. if chatid>0:
  83. data=vk.messages.getChat(chat_id=chatid)
  84. #l_name = "{"+n['title']+"}"
  85. c_name = data['title']
  86.  
  87. else:
  88. f_name, l_name = group_parse(user)
  89.  
  90. #if len(txt) == 0: txt="Вложение"
  91. send_notify(f_name, l_name, c_name, txt, attach)
  92.  
  93.  
  94.  
  95. def user_parse(uid):
  96. global vk
  97. data = vk.users.get(user_ids=uid)
  98. fn=data[0]['first_name']
  99. ln=data[0]['last_name']
  100. return fn, ln
  101.  
  102. def group_parse(uid):
  103. global vk
  104. data = vk.groups.getById(group_ids=uid)
  105. fn=data[0]['name']
  106. ln=""#data[0]['screen_name']
  107. return fn, ln
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. def main():
  119. global vk
  120. """ Пример использования longpoll
  121. https://vk.com/dev/using_longpoll
  122. https://vk.com/dev/using_longpoll_2
  123. """
  124.  
  125. print("start")
  126. login, password = 'login', 'password'
  127. vk_session = vk_api.VkApi(login, password)
  128.  
  129. vk = vk_session.get_api()
  130.  
  131.  
  132. try:
  133. vk_session.auth(token_only=True)
  134. except vk_api.AuthError as error_msg:
  135. print(error_msg)
  136. return
  137.  
  138. longpoll = VkLongPoll(vk_session)
  139. try:
  140. # while 1:
  141. for event in longpoll.listen():
  142.  
  143. # print("pre onl")
  144. # if time() > onl+300:
  145. # vk.account.setOnline(voip=0)
  146. # onl=time()
  147. # print("onl "+str(onl))
  148.  
  149. if event.type == VkEventType.MESSAGE_NEW:
  150. #print('New message:')
  151. attach=""
  152. chatid=0
  153. if event.from_me:
  154. #print('From ME to: ', end='')
  155. user=['user', -1]
  156. elif event.to_me:
  157. try:user=['user', event.user_id]#'For me from: '#, end=''
  158. except:user=['group', event.group_id]#'For me from: '#, end=''
  159.  
  160. if 'fwd_count' in event.raw[6]:
  161. attach=" {пересланные: "+event.raw[6]['fwd_count']+"}"
  162.  
  163. elif len(event.raw[7])>0:
  164. print(event.raw[7])
  165. if 'attach1_kind' in event.raw[7]:
  166. attach=" {"+attachments[event.raw[7]['attach1_kind']]+"}"
  167. elif 'attach1_type' in event.raw[7]:
  168. attach=" {"+attachments[event.raw[7]['attach1_type']]+"}"
  169. elif 'geo_provider' in event.raw[7]:
  170. attach=" {"+"карта"+"}"
  171.  
  172.  
  173. #else: attach=""
  174. if str(event.raw[3]).startswith('200'):
  175. tmp=str(event.raw[3])
  176. chatid=int(tmp[tmp.rfind('0')+1:])
  177. # else:chatid=0
  178.  
  179. #if event.from_user:
  180. # print(event.user_id)
  181. elif event.from_chat:
  182. print(event.user_id, 'In beseda1', event.chat_id)
  183. #elif event.from_group:
  184. # print('Groups', event.group_id)
  185.  
  186. txt=event.text
  187. response(txt, user, attach, chatid)
  188. #print()
  189.  
  190. # elif event.type == VkEventType.USER_TYPING:
  191. # print('Printing ', event.user_id)
  192.  
  193. # if event.from_user:
  194. # print(event.user_id)
  195. # elif event.from_group:
  196. # print('Admin group', event.group_id)
  197.  
  198. # elif event.type == VkEventType.USER_TYPING_IN_CHAT:
  199. # print('Printing ', event.user_id, ' in beseda', event.chat_id)
  200.  
  201. # elif event.type == VkEventType.USER_ONLINE:
  202. # print('User', event.user_id, 'online', event.platform)
  203.  
  204. # elif event.type == VkEventType.USER_OFFLINE:
  205. # print('User', event.user_id, 'offline', event.offline_type)
  206.  
  207. # else:
  208. # print(event.type, event.raw[1:])
  209. # try:print(event.type, event.raw[1:])
  210. # except:print('ascii error')
  211.  
  212.  
  213. sleep(0.01)
  214. except:
  215. pass
  216.  
  217. #onl=time()-301
  218. #2397354_109347657_1a8539d301563350ad
  219. # Если нас запускают, а не подключают к какому то проекту то...
  220. if __name__ == '__main__':
  221. # Запускаем главную функцию
  222. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement