HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准。

HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。

 电报群:https://t.me/joinchat/FZtb9xRFeUHu0hxiRMQqCA
# -*- coding: utf-8 -*-
# telegram_bot_http.py

import urllib3
import logging

g_http_chat_id = -340097345


def check_http(bot, job):
    http = urllib3.PoolManager()
    http_url = 'http://ninthclan.com/feed/'
    http_get = http.request('Get', http_url)
    if 200 != http_get.status:
        error_msg = 'error url : [%s](%s) \nstatus : %d' % (http_url, http_url, http_get.status)
        logging.info('error_msg : %s' % error_msg)
        bot.send_message(chat_id=g_http_chat_id, text=error_msg, parse_mode='Markdown')
    logging.info('check_http job : %s Sleeping for 30 mins...' % job)


def start_http(job):
    logging.info('start_http')
    job.run_repeating(check_http, interval=30 * 60, first=0)
# -*- coding: utf-8 -*-
# telegram_bot_jobs.py

import logging
from telegram.ext import Updater
from datetime import datetime

import telegram_bot_http


def error(bot, update, err):
    logging.error('bot : %s update : %s error : %s' % (bot, update, err))


def echo(bot, update):
    logging.info("bot : %s chat_id:%d text:%s" % (bot, update.message.chat_id, update.message.text))


def start_bot():
    try:
        start_time = datetime.now()  # 获取当前datetime
        logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

        # 温顺的莱昂
        updater = Updater(token='739344882:AAF_BMyjc7S45nado1dK5E6sMt-0jYH5VMA')
        dp = updater.dispatcher
        job = updater.job_queue
        dp.add_error_handler(error)

        # #3 电报机器人-HTTP接口监听
        telegram_bot_http.start_http(job)

        updater.start_polling()
        logging.info('telegram bot updater start_polling...')
        updater.idle()

        end_time = datetime.now()  # 获取当前datetime
        logging.info('花费时间:%f秒' % (end_time - start_time).total_seconds())
    except BaseException as start_bot_ex:
        logging.error("telegram_bot_jobs ex : %s" % start_bot_ex)

发表评论

电子邮件地址不会被公开。 必填项已用*标注