策略知识库.EN
策略

Telegram-发信接口-V200-Python

FawkesPan · python · 工具与附录 · 2019-05-16 20:06:03

全部策略 →

背景术语

概述

Telegram API接口 (FMZ.com)

##### 这个库目前只能Telegram发信 后续会增加更多功能。

其他

```

key 为 Bot key

chat_id 为收信用户Telegram ID

Telegram = ext.Telegram(key=string, chat_id=integer) # 创建一个新的接口对象 `` ``

chat_id 为可选项 默认会使用初始化时设置的ChatID

message 即为信息内容

Telegram.Send(message=string, chat_id=integer) ``` 在能够给自己发信息之前,你需要先在Telegram中给自己的机器人发送一条 /start 信息,不然机器人是没法给你发信的。 邮箱 i@fawkex.me 电报 [FawkesPan]( 接受策略定制 [Telegram API文档]( [使用 WTFPL – Do What the Fuck You Want to Public License](

参数

参数默认值说明
DEBUG0DEBUG模式: 0
KEYTelegram 机器人 Key
CHATIDfalseTelegram 会话 ID
源码(51 行)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# encoding: utf-8
# 
# Telegram Interface for FMZ.com
#
# Copyright 2018 FawkesPan
# Contact : i@fawkex.me / Telegram@FawkesPan
#
# Do What the Fuck You Want To Public License
#

try:
    import requests
except:
    print('Requests not installed. Try: pip install requests')
    Log('Requests not installed. Try: pip install requests')
    
    raise Exception('Requests not installed. Try: pip install requests')

class Telegram:
    def __init__(self):
        self.key = KEY
        self.chat_id = CHATID
        self.url = 'https://api.telegram.org/bot%s' % self.key
        
    def Send(self, message='', chat_id=None):
        if chat_id is None:
            chat_id = self.chat_id
        PARAM = {}
        PARAM['chat_id'] = chat_id
        PARAM['text'] = message
        PARAM['parse_mode'] = 'markdown'
        URL = self.url + '/sendMessage'
        try:
            res = requests.post(URL, data = PARAM)
            return True
        except IOError as e:
            print(e)
            return False
        

ext.Telegram = Telegram

# 模块功能测试
def main():
    if DEBUG == 1:
        msger = ext.Telegram(KEY, CHATID)
        msger.Send("Hello World!")
    
    return True