Hi All!
Today i was played a little with telegram bots and notifications from tactic. Want to share some raw code, to anyone who's interesting.
This trigger on change|sthpw/task
Also, wanted to ask devs. Does it possible to run telegram bot polling from inside tactic or it is not recommended? It would be great to have ability to access all TACTIC from itself, not through slow xmlrpc api.
And some code for mass notifications:
Today i was played a little with telegram bots and notifications from tactic. Want to share some raw code, to anyone who's interesting.
This trigger on change|sthpw/task
Code:
import telegram
bot = telegram.Bot(token='TOKEN_HERE')
"""
#***LOGGING
from pprint import pformat
assigned_user_chat_id = CHAT_ID_HERE
mess = pformat(input)
message = bot.send_message(chat_id=assigned_user_chat_id, text=mess)
#***LOGGING
"""
def sending_guess(status=None):
# some code to guess whos should be notified
send_to_user = False
send_to_supervisor = False
return send_to_user, send_to_supervisor
def get_users_from_task_sobj(sobj):
assigned_user = sobj.get('assigned')
supervisor_user = sobj.get('supervisor')
if assigned_user:
assigned_user = server.eval("@SOBJECT(sthpw/login['login','{0}'])".format(assigned_user))[0]
if supervisor_user:
supervisor_user = server.eval("@SOBJECT(sthpw/login['login','{0}'])".format(supervisor_user))[0]
return assigned_user, supervisor_user
assigned_user, supervisor_user = get_users_from_task_sboj(input['sobject'])
assigned_user_chat_id = None
if assigned_user:
assigned_user_chat_id = assigned_user['phone_number']
supervisor_user_chat_id = SUPER_CHAT_ID
if supervisor_user:
supervisor_user_chat_id = supervisor_user['phone_number']
def generate_task_info_message(input, assigned_user):
message_text = None
if input['mode'] == 'insert':
status = input['update_data'].get('status')
task_sobject = input['sobject']
filters = [('code', task_sobject['search_code'])]
related_sobject = server.query(task_sobject['search_type'], filters, single=True)
message_text = u'{0}: {1}\nПроцесс: {2}\nОписание:{3}\nКатегория: {4}\nПроект: {5}'.format(status, related_sobject['name'], task_sobject['context'],related_sobject['description'],related_sobject['assets_category_code'], task_sobject['project_code'])
if input['mode'] == 'update':
status = input['update_data'].get('status')
task_sobject = input['sobject']
filters = [('code', task_sobject['search_code'])]
related_sobject = server.query(task_sobject['search_type'], filters, single=True)
gallery_link = 'http://SITE/episodes/{}'.format(related_sobject['code'])
assigned_user_title = u'{0} {1}'.format(assigned_user['first_name'], assigned_user['last_name'])
message_text = u'{0}: {1}\nИсполнитель: {2}\nПроцесс: {3}\nПроект: {4}\nГалерея: {5}'.format(status, related_sobject['name'], assigned_user_title, task_sobject['context'], task_sobject['project_code'], gallery_link)
return message_text
message_text = generate_task_info_message(input, assigned_user)
send_to_user, send_to_supervisor = sending_guess()
if message_text:
# sendging message to supervisor
if supervisor_user_chat_id and send_to_supervisor:
message = bot.send_message(chat_id=supervisor_user_chat_id, text=message_text)
# sendging message to assigned user
if assigned_user_chat_id and send_to_user:
message = bot.send_message(chat_id=assigned_user_chat_id, text=message_text)
Also, wanted to ask devs. Does it possible to run telegram bot polling from inside tactic or it is not recommended? It would be great to have ability to access all TACTIC from itself, not through slow xmlrpc api.
And some code for mass notifications:
Code:
# **** making mass notification about free episode ****
# first we check if animation or layout isn't assigned, for that we get those tasks
PROCESSES = ['script', 'layout', 'animation']
have_assigned_tasks = False
have_approved_script = False
for process in PROCESSES:
search_task = Search('sthpw/task')
search_task.add_sobject_filter(sobject)
search_task.add_filter('process', process)
task_sobject = search_task.get_sobject()
if task_sobject:
if process == 'script':
if task_sobject.get_value('status') in [u'Принят', u'Оплачен']:
have_approved_script = True
else:
if task_sobject.get_value('assigned'):
have_assigned_tasks = True
mess = '{}/{}'.format(have_assigned_tasks, have_approved_script)
# then if we have False/True we create mass notification
# for that, we getting assigned groups of logins
animators_list = []
if not have_assigned_tasks and have_approved_script:
animators_logins = Search.eval("@SOBJECT(sthpw/login_in_group['login_group','animator'].sthpw/login)")
for animator_login in animators_logins:
if animator_login.get_value('phone_number'):
animators_list.append(animator_login)
# when we have list with logins we can generate mass message
def generate_message(parent_sobject):
episode_link = 'http://SITE/link/free_to_take_episodes'
episode_script = parent_sobject.get_value('script')
episode_name = parent_sobject.get_value('name')
mess = '<i>Готов к работе:</i><b> {0}</b>\n<a href="{1}">Сценарий</a>\n<a href="{2}">Свободные эпизоды</a>'.format(episode_name, episode_script, episode_link)
return mess
# begin mass sending
for animator in animators_list:
if animator.get_value('phone_number'):
try:
bot.send_message(chat_id=animator.get_value('phone_number'), parse_mode=telegram.ParseMode.HTML, text=generate_message(sobject))
except:
mess = 'Message not sent to {0}'.format(animator.get_value('login'))
bot.send_message(chat_id=CHECK_ID, text=mess)