最有效腾讯云Python3推送TG之Hello World

前言

Python模块不用考虑未知BUG,源码有很多BUG要踩,推荐python-telegram-bot模块。腾讯无服务器云函数 Serverless Cloud Function (SCF)官网https://cloud.tencent.com/product/scf

Python模块

创建SCF

https://console.cloud.tencent.com/scf/list

自定义创建

事件函数

代码部署

Python3.6

python-telegram-bot

python-telegram-bot/python-telegram-bot: We have made you a wrapper you can't refuse (github.com)

安装依赖

1
2
cd src
python3 -m pip install -i https://pypi.python.org/simple python-telegram-bot==13.7 -t .

ps:只有清华源https://pypi.python.org/simple和官方源有python-telegram-bot

index.py代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf8 -*-
import telegram

def main_handler(event, context):
TOKEN='ssss'
text='Hello World'
chat_id='2222'
bot = telegram.Bot(token=TOKEN)
try:
bot.send_message(text=text, chat_id=chat_id)
text='a' * telegram.constants.MAX_MESSAGE_LENGTH
bot.send_message(text=text, chat_id=chat_id)
text='a' * (telegram.constants.MAX_MESSAGE_LENGTH+1)
bot.send_message(text=text, chat_id=chat_id)
except Exception as e:
err = str(e)
bot.send_message(text=err, chat_id=chat_id)

if __name__ == "__main__":
main_handler("","")

telepot

什么是 Telepot ?

Telepot 是由 Nike Lee (github ID nickoala)开发的整合所有了telegram API并方便开发者使用的python packge,通过安装 telepot我们可以直接在python中使用telegram API的功能。

nickoala/telepot: Python framework for Telegram Bot API (github.com)

安装依赖

1
2
cd src
pip install telepot -t .

index.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- coding: utf8 -*-
import telepot #将telepot这个模组导入到Python中来

def main_handler(event, context):
bot_api=jsljfaljdf
bot = telepot.Bot(bot_api) #将bot这个在程序中使用的variable和你的bot token联系起来,在之后的程序中,每当要命令bot的时候可以直接call bot的instance
print(bot.getMe())
chat_id='jaljflajslkjfl'
text='Hello World'
bot.sendMessage(chat_id, text)

if __name__ == "__main__":
main_handler("","")

源码

创建SCF

https://console.cloud.tencent.com/scf/list

自定义创建

事件函数

代码部署

Python3.6

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf8 -*-
import os
import json
import requests
import time
from urllib import parse

# TG推送
def TGPost(bot_api, chat_id, text):
print('开始推送')
headers = {
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'}
text = parse.quote(text)
# 修改为自己的bot api token
post_url = 'https://api.telegram.org/bot{}/sendMessage' \
'?parse_mode=MarkdownV2&chat_id={}&text={}'.format(bot_api, chat_id, text)
try:
requests.get(post_url, headers=headers)
print('推送成功')
except Exception:
print("推送失败")
time.sleep(3)
# 避免推送死循环
try:
requests.get(post_url, headers=headers)
except Exception:
pass

def main_handler(event, context):
print("Received event: " + json.dumps(event, indent = 2))
print("Received context: " + str(context))
bot_api = os.environ['BOT_TOKEN']
chat_id = os.environ['CHAT_ID']
TGPost(bot_api,chat_id,"Hello World")
print("Hello world")
return("Hello World")


if __name__ == "__main__":
main_handler("","")

环境配置

TGtoken获取方式

1
2
BOT_TOKEN 在@BotFather申请,
CHAT_ID可以通过机器人@userinfobot发送任意消息获取,返回的id即是CHAT_ID

环境变量

1
2
BOT_TOKEN
CHAT_ID

编辑方法

image-20211027115103684

结果

TG成功Hello World