《steem 指南》——12.3.2 提醒机器人开发示例(1)——一个简单VP回复提醒器

in #cn6 years ago (edited)

提醒机器人在steem是一类很实用的机器人。开发起来也不会太复杂,只要你想好自己需要提醒的内容,并想好提醒的方式,就可以轻易的实现。

那我们来一起写一个简单的提醒机器人吧!


作为示例,我们使用python写一个简单的VP回复提醒器。当我们VP恢复到99%以上的时候,发出邮件提醒。

程序逻辑如下:


我们调用@justyy的API,快速查询VP,当然也可以使用python-steem库

id="weicomenew"  #查询谁的ID

get_vp_base_url = 'https://uploadbeta.com/api/steemit/account/vp/?cached&id=%s'
get_vp_url = get_vp_base_url % id
r = requests.get(get_vp_url)
vp = float(r.text)

然后加上一个判断条件:VP大于99就发送邮件通知。

if vp>= 99:

然后这里我们先编写邮件内容:

title = "你的账号:" + id + ",VP已恢复提醒!"

content = "你的账号是:" + id + \
          "\n当前VP为:" + str(vp) +  \
          "\nVP已恢复!快去点赞吧!"

之后就可以发送邮件了,我这里使用的是zmail的依赖库,大家可以自由选择自己喜欢的。使用其他方式提醒也是可以的,比如可以接入微信公众号提醒等。

后面加上VP如果小于99,就等待再次执行等条件。


    if vp>= 99:
            mail_content = {
                "subject": title,
                "content": content,}

            print(mail_content)
            # 使用你的邮件账户名和密码登录服务器
            server = zmail.server("[email protected]","yu803636")
            server.send_mail("[email protected]",mail_content)
            print("邮件已发送")
            time.sleep(3600)
    else:
        print("VP还没恢复,等待。"+"当前VP为:"+str(vp))
        times = int((99 - vp) * 0.8333 * 60 * 60)
        time.sleep(times)

我们运行发送一次看看。

很好的发送成功了,这个小机器人就运行起来了。

我们可以发挥想象力,让机器人提醒更多的内容。比如有新回复,新点赞,关注的人有新文章等等。
提醒的方式也可以多种多样,可以是邮件,短信,APP,公众号等的提现,怎么用就看你发挥了。

下面放全部的源代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import datetime
import requests
import zmail

p=1
id = "weicomenew"  # 查询谁的ID

while p==1:
    get_vp_base_url = 'https://uploadbeta.com/api/steemit/account/vp/?cached&id=%s'
    get_vp_url = get_vp_base_url % id
    r = requests.get(get_vp_url)
    vp = float(r.text)

    title = "你的账号:" + id + ",VP已恢复提醒!"

    content = "你的账号是:" + id + \
              "\n当前VP为:" + str(vp) +  \
              "\nVP已恢复!快去点赞吧!"



    if vp>= 99:
            mail_content = {
                "subject": title,
                "content": content,}

            print(mail_content)
            # 使用你的邮件账户名和密码登录服务器
            server = zmail.server("发送者邮箱","密码")
            server.send_mail("接受邮箱",mail_content)
            print("邮件已发送")
            time.sleep(3600)
    else:
        print("VP还没恢复,等待。"+"当前VP为:"+str(vp))
        times = int((99 - vp) * 0.8333 * 60 * 60)
        time.sleep(times)
Sort:  

不错的技术文!

要不要来个完全小白的操作流。包括怎么部署😳

部署在前几节教程有写

看了你这文章,我也打算自己写个机器人程序了

so cool, bookmarked.
一直想寫小程序, 但遲遲懶惰未開始

闹钟都能让我在睡梦中按掉的,提醒好像没什么卵用啊

Posted using Partiko iOS

厉害👍感谢分享😊

Posted using Partiko Android

YOU JUST GOT UPVOTED

Congratulations,
you just received a 13.97% upvote from @steemhq - Community Bot!

Wanna join and receive free upvotes yourself?
Vote for steemhq.witness on Steemit or directly on SteemConnect and join the Community Witness.

This service was brought to you by SteemHQ.com

Hi @maiyude!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.484 which ranks you at #6566 across all Steem accounts.
Your rank has dropped 25 places in the last three days (old rank 6541).

In our last Algorithmic Curation Round, consisting of 275 contributions, your post is ranked at #60.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • The readers appreciate your great work!
  • You have already shown user engagement, try to improve it further.

Feel free to join our @steem-ua Discord server

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 57972.96
ETH 2357.13
USDT 1.00
SBD 2.36