如何在独特的帖子中发布slackbot帖子通知

2024-09-27 07:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的Python代码。基本上,它连接到SQL Server数据库,并获得一个通知列表,以便在空闲通道上发布。多个通知-如果它们的时间在近距离内-有时作为消息一起发布到Slack通道

当前输出为

User John submitted new notification. Link:

User John submitted new notification. Link:
User Jane submitted new notification. Link:
User Max submitted new notification. Link:

Python代码:

import requests
import json
import adodbapi
import adodbapi.ado_consts as adc
import adodbapi.is64bit as is64bit

sqlconn = adodbapi.connect('Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security 
Info=False;Initial Catalog="MyDatabase";Data Source=localhost,1360');
cur = sqlconn.cursor()
cur.execute('''
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @PostedOn datetime
SELECT @PostedOn=LastChecked FROM Notifications
select Email,a.PostedOn, '<https://www.website.com/' +CASE WHEN a.QuestionId IS NOT NULL THEN 
'question' ELSE 'article' END+'/'+CONVERT(varchar,COALESCE(A.questionId,A.ArticleId))+'|Link>'
from UserProfile u
inner join answer a on a.UserId=u.UserId
where exists(select * from Topic et where et.UserId=u.UserId)
AND u.IsExpert=1
AND A.Postedon>@PostedOn

update Notifications
set LastChecked=GETDATE()
''')


sqlconn.commit()
url = 'http://ipaddress:port/bot/chatsecrets/notification-network'
headers = {'content-type': 'application/json'}
hasrows=0
postlist=''
template ='''User {0} submitted new notification. {1}:'''
for row in cur.fetchall():
    postlist = postlist +'\n'+template.format(row[0],row[2])
    hasrows=1

if hasrows:
    response = requests.post(url, data=json.dumps({'secret': postlist}), headers=headers)

print (postlist)

我想配置代码,以便bot可以为每个新通知向Slack发送一条新消息,而不是收集通知列表并将其作为批量更新发送


Tags: 代码importjsonnewlinknotificationheadersuserid

热门问题