我想通过电报从Python脚本中发送消息。我已经尝试过通过电报-cli( 维生原版和“幸运”的补丁版本 )来做到这一点.有了他们,我可以成功地把信息发送到我的手机。我的问题是:
<<EOF ... EOF
输入失败,因为这个所以问题失败了;程序在控制台上打开,但没有输出任何内容。<<EOF
)。
#/bin/bash to=Matthias_SG msg="test message“tgpath=/home/ Matthias /dvl/tg cd ${tgpath} (回波"add_contact +xxx Matthias SG";echo "msg $to $msg")所以我的问题是:我应该回到老矮人身上吗?我可以通过从stringIO或popen中输入subprocess.call来修复shell脚本或将它们修改为Python吗?有没有人以一种强有力的方式使用这个?
背景
发布于 2019-11-22 05:11:03
第一步是创建一个bot并获取token
。
第二步是获取chat_id
https://api.telegram.org/bot<YourBOTToken>/getUpdates
并获取密钥message['chat']['id']
下的chat_id
。最后一步是使用以下代码:
import requests
def telegram_bot_sendtext(bot_message):
bot_token = ''
bot_chatID = ''
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
test = telegram_bot_sendtext("Testing Telegram bot")
print(test)
发布于 2022-03-30 16:13:12
@BotFather
谈谈。向他们发送/newbot
并按照提示进行操作。作为响应,您将得到一个HTTP令牌(它看起来类似于123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
)。/start
即可。https://api.telegram.org/bot<token>/getUpdates?offset=-1
(从BotFather收到的标记按字面粘贴,包含所有字母和标点符号)。从返回的JSON对象复制chat id。发布于 2017-05-23 15:53:11
我宁愿使用包python-telegram-bot
,它对我来说很好。
您可以找到这里文档和一个简单的入门示例。
为了回复文本消息,可以在MessageHandler之后添加CommandHandler,例如:
updater.dispatcher.add_handler(MessageHandler(Filters.text, text_reply))
def text_reply(bot, updater):
text = update.message.text
if text == 'ping':
reply = 'pong'
elif text == 'pong':
reply = 'ping'
# add any process to the text
else:
reply = "I only respond to ping pong"
update.message.reply_text(reply)
不要忘记导入from telegram.ext import MessageHandler
。
希望这能帮上忙!
https://stackoverflow.com/questions/29003305
复制相似问题