One, the origin

A few days ago, my girlfriend -> my girl friend sent me the picture above, saying that she also wanted to receive sweet rainbow farts every day. Boy, I thought it was just a rainbow fart bot, I lied to myself. But since she opened her mouth, that we must have to give her the whole place, go ๐Ÿš€

Two, preparation work

Considering her recently in preparation for one’s deceased father grind, need someone to encourage her every day, so I’m going to give her something to poison chicken soup every day, and then do a prompt, one’s deceased father grind the countdown to check the weather that day, and remind her cold add clothes, be careful catch cold catch a cold, thought of here I have moved to), warm warm bot, very sweet, ha, ha, ha.

The whole a weather query interface from the Internet first: http://wthrcdn.etouch.cn/weather_mini?city=

OK, now the weather has, the temperature also know, the following again the whole rainbow fart interface:

https://chp.shadiao.app/api.php (thanks to shadiao app | 2333. JPG)

And then give a poison chicken soup interface friendship: https://www.iowen.cn/jitang/api/

OK, everything is ready, code whole ๐Ÿ˜Ž

Life is short. I use Python

No time to explain, get in the car ๐Ÿš—

# coding=utf-8

import io
import sys
import time
import datetime

import requests

from email.mime.text import MIMEText
from email.header import Header
import smtplib

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

mail_host = "smtp.163.com"
mail_user = "[email protected]" # Fill in your email here
mail_girl = "[email protected]" # your girlfriend's email
mail_pass = "xxxxxxx" # Email password
name_girl = "xxx" # your girlfriend's name
mail_port = 465

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')

def getWeather() :
    r = requests.get(
        'http://wthrcdn.etouch.cn/weather_mini?city=%E6%9B%B2%E9%98%9C')
    if 'OK' in r.text:
        r = r.json()['data']
        tip = r['ganmao']
        temperature = r['wendu']
        weather = r['forecast'] [0] ['type']
        return F 'weather:{weather}Current temperature:{temperature}\n{tip}'
    return 'failed to get weather :(')


def getTime() :
    today = datetime.datetime.now()+datetime.timedelta(hours=+8)
    date2 = time.strptime('2020-12-25'."%Y-%m-%d")
    date2 = datetime.datetime(date2[0], date2[1], date2[2])
    t = today.timetuple()
    days = (date2-today).days
    return F 'is today{t.tm_year}years{t.tm_mon}month{t.tm_mday}Day of week{t.tm_wday+1}Countdown to postgraduate entrance exam:{days}Days'


def getSoup() :
    r = requests.get('https://www.iowen.cn/jitang/api/')
    if 'Data obtained successfully' in r.text:
        return r.json()['data'] ['content'] ['content']
    return The college entrance examination was yesterday, the postgraduate entrance examination is tomorrow, today has nothing to do. '


def getPi() :
    r = requests.get('https://chp.shadiao.app/api.php').text
    return r if len(r) > 3 else 'You must have been a carbonated drink in a previous life. Why do I always bubble with joy when I see you?'


def morning() :
    return '\n'.join([getTime(), getSoup(), getWeather(), getPi()])


def night() :
    return '\n'.join([getSoup(), getPi(), F 'good night,{name_girl}Classmate, today you are also the best, continue refueling duck! '])


def getTimeX() :
    t = int(time.strftime("%H", time.localtime()))+8
    if t > 24:
        t = t-24
    return 'morning' if t < 11 else ('noon' if t < 17 else 'afterNoon')


def main_handler(event, context) :
    smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)
    try:
        smtpObj.login(mail_user, mail_pass)
    except smtplib.SMTPException as e:
        logger.info(e)
        return False
    content = night() if getTimeX() == 'afterNoon' else morning()
    message = MIMEText(content, 'plain'.'utf-8')
    message['From'] = mail_user
    message['To'] = mail_girl
    message['Subject'] = F 'good morning,{name_girl}' ifgetTimeX() ! ='afterNoon' else 'Good night, XXX'
    try:
        smtpObj.sendmail(mail_user, [mail_girl], message.as_string())
        logger.info("send email success")
    except smtplib.SMTPException as e:
        logger.info(e)
        logger.info("Error: send email fail")
    logger.info(content)
    return content
Copy the code

Serverless helps you

The whole above those almost two minutes in the past, don’t worry, left a minute to Tencent cloud function enough โ˜•

Sign up for Tencent Cloud (Cloud.tencent.com/)

Step 2: open the tencent cloud console (console.cloud.tencent.com/)

Step 3: Open the Cloud Function menu and choose Function Services > Region (any one) > New

Step 4: Create a Python cloud function, python3.6, select a blank function template, and go to the next Step

Step 5: Copy and paste the previous Python function into index.py (overwrite the original code)

Step 6: Finally, add a scheduled trigger

Open the newly created bot cloud function, select trigger management, create a trigger, select timing trigger, and customize the trigger cycle: 0 30 6 * * * *, that is, at 6:30 am (Beijing time) every day, the current function will be automatically triggered to run once, in order to send “rainbow fart” to the girlfriend at a certain time every day ๐Ÿ˜€

Here I morning and evening each added a timing trigger, every day to the female ticket not only morning afternoon good night ๐Ÿ˜œ

PS: Tencent cloud function monthly free 1 million times + 400000 GBยทs resource usage oh, super conscience have no, touched ~

Come on, show

Good morning,

Good night

Six, summarized

Above that, we simply implemented a salt-and-sweet Rainbow Fart Bot,

No need to worry about forgetting the morning, afternoon and night with the girlfriend, ha, ha, ha, ha

PS: the above plot is purely fictional, the building master temporarily no female ticket, single can hold up, the dog head life ๐Ÿถ

Seven, other

I started a column on Tencent Cloud + called “Everything Can Be Serverless.”

Those of you who are interested in some interesting Serverless practices can take a look.

Everything Can Serverless column series

  1. Build your own unlimited large capacity cloud disk (5TB) for free
  2. Everything can be Serverless using the cloud function Timer trigger to achieve automatic timing every day
  3. Serverless can use SCF+COS to quickly develop full stack applications
  4. Everything can be Serverless using SCF+COS free wechat public account operation
  5. Everything can be Serverless using SCF rapid deployment verification code identification interface
  6. Everything can be identified by Serverless Kaggle+SCF end-to-end verification code from training to deployment
  7. Everything can be easily managed by Serverless via wechat official account
  8. Write a letter to the future using SCF+COS
  9. Everything can be Serverless fast access to Tencent Cloud development in Flutter
  10. Write a Dart native Tencent Cloud object storage plug-in in Flutter
  11. Everything can be Serverless about the cloud function cold and hot to start those things
  12. Everything can be Serverless my Serverless way

๐Ÿ† issue 7 | all things can be Serverless technology projects