iT邦幫忙

2021 iThome 鐵人賽

DAY 30
0
Software Development

LINE Messaging API SDK for Python 實現群組團購輔助機器人系列 第 30

DAY 30完成團購機器人

前面的設置中文字體,發現不能在heroku上使用
這邊我用matplotlib.font_manager 的 FontProperties直接使用指定路徑的字體來代替

fontname='font/Arial Unicode MS.ttf'
for key, cell in tab.get_celld().items():
    row, col = key
    if row >= 0 and col >= 0:
        cell.set_text_props(fontproperties=FontProperties(fname=fontname))

修改後

if(event.message.text[:3:]=="團購" and rights[2]): #view right
    cursor = conn.cursor()
    cursor.execute(f"SELECT keyword,name,quantity FROM group_buying_user NATURAL JOIN group_buying_message order by keyword;")
    users = cursor.fetchall()
    cursor.close()
    fig, ax =plt.subplots(1,1)
    data=[]
    color=[]
    color_list = ["#89C6C6","#A889C6","#C68989","#A8C689","#C07BC0","#C6C689"]
    category=''
    i=0
    column_labels=['keyword','name','quantity']
    for user in users:
        if not category==user[0]:
            if i==len(color_list)-1:
                i=0
            else:
                i=i+1
        color.append([color_list[i],color_list[i],color_list[i]])
        category=user[0]
        data.append([user[0],user[1],user[2]])
    ax.axis('tight')
    ax.axis('off')
    tab=ax.table(cellText=data,colColours=["#DDDDDD"]*3,cellColours=color,colLabels=column_labels,cellLoc='center',loc="center")
    fontname='font/Arial Unicode MS.ttf'
    for key, cell in tab.get_celld().items():
        row, col = key
        if row >= 0 and col >= 0:
            cell.set_text_props(fontproperties=FontProperties(fname=fontname))
    plt.savefig('img/table.png',dpi=200)
    CLIENT_ID='ceda715b6015fcb'
    PATH = "img/table.png"
    im = pyimgur.Imgur(CLIENT_ID)
    uploaded_image = im.upload_image(PATH, title="iThome-2021")
    print(uploaded_image.link)
    message = ImageSendMessage(
        original_content_url=uploaded_image.link,
        preview_image_url=uploaded_image.link
    )
    line_bot_api.reply_message(event.reply_token,message)

推送至Heroku後功能測試

新增團購訊息

https://ithelp.ithome.com.tw/upload/images/20211012/20140165eW38tSfSdL.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165ofgzveiOra.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165pkggyfem6z.png

我要+1

https://ithelp.ithome.com.tw/upload/images/20211012/201401656sbWuZ6jxB.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165vrEetF3i8K.png
更新數量
https://ithelp.ithome.com.tw/upload/images/20211012/20140165g7Z4wstFGx.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165P4BlBsVE5T.png

手動新增團購者

https://ithelp.ithome.com.tw/upload/images/20211012/201401653JvCekF88K.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165HLyUxZZ66e.png

手動刪除團購者

https://ithelp.ithome.com.tw/upload/images/20211012/20140165yN9JYxX6yu.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165adbsMz6jS3.png

查詢團購訊息

https://ithelp.ithome.com.tw/upload/images/20211012/20140165nCeQ3eXilE.png

刪除團購訊息

https://ithelp.ithome.com.tw/upload/images/20211012/201401651JqvM1ET5G.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140165WcuebFcuYM.png


上一篇
DAY 29 製作表格-為表格上色
系列文
LINE Messaging API SDK for Python 實現群組團購輔助機器人30

尚未有邦友留言

立即登入留言