telegram bot: how should i get user feedback after forcereply? - python

#!/usr/bin/env python3
#-*- encoding: utf-8 -*-
from apikey import tgbottoken,authedchat
from telebot import types
import telebot,logging
bot =telebot.TeleBot(tgbottoken)
telebot.logger.setLevel(logging.DEBUG)
def extract_arg(arg):
return arg.split()[1:]
#bot.message_handler(commands=['newmail'])
def mailwithsg(msg):
cid = msg.chat.id
sendto = types.ForceReply(selective=False)
bot.send_message(cid, "Send me another word:", reply_markup=sendto)
bot.polling(none_stop=True)
after I use the methods and send a reply markup message, how should I get user's reply text (like user feedback)? which method should I use?
I now use pyTelegramBotAPI as a Python Wrapper.

It will reply a normal message.
Only Inline Keyboard should use CallBackQuery to handle it.
So that just use message_id to identify all messages and do what you want.

use my method from php - i think you can translate it to python;
switch($reply_to_message_text)
case 'Send me another word:':
$usersreply = $message ;
$this->db->addfeedback($user_id,$usersreply)
--------->here you can send some text to user and say Thanks)

Related

Posting a message in Symphony from Python

I'm trying to send a message from Symphony using Python.
https://developers.symphony.com/restapi/reference#create-message-v4
I found this page but I don't really know how to use it ( There's a cURL and a post url .. ) and I don't understand how I can use requests in this context ( still a beginner in API ).
Can someone help me to figure out how I can use this page to send a message from Python.
Thank you
You have to pass some required things in headers and use multipart/from-data content-type.
If you know about the postman then first with that and pass required headers.
files ={"message":"<messageML>Hello world!</messageML>"}
headers={
"sessionToken": "SESSION_TOKEN",
"keyManagerToken": "KEY_MANAGER_TOKEN"
}
requests.post("https://YOUR-AGENT-URL.symphony.com/agent/v4/stream/:sid/message/create", files=files,headers=headers)
https://github.com/finos/symphony-bdk-python/blob/main/examples has alot of examples on how to use symphony sdk. From python you don't want to use the api. This is the simple code to send a message from a bot. If you do not have a bot set up yet, follow https://docs.developers.symphony.com/building-bots-on-symphony/configuration/configure-your-bot-for-bdk-2.0-for-python NOTE you will need an admin user to follow these steps.
from symphony.bdk.core.config.loader import BdkConfigLoader
from symphony.bdk.core.symphony_bdk import SymphonyBdk
config = BdkConfigLoader.load_from_file("config.yaml")
async with SymphonyBdk(config) as bdk:
streams = bdk.streams()
messages = bdk.messages()
user_id = 123 # this can be found by clicking on a profile and copy profile link eg symphony://?userId=31123123123
stream = await streams.create_im_or_mim([user_id])
await messages.send_message(stream.id, f"<messageML>Message you want to send</messageML>")

python telegram bot: why are digits a link to phone number?

Sending text/markdown with Python-Telegram-Bot, all digit groups are rendered as links to phone numbers like in
Clicking on 12345 it jumps to the phone call keyboard. How can I avoid this behavior, rendering 12345 as normal text (not a link)?
I went through the documentation but cannot find any reference to this particular issue.
This is the test code:
import telegram
from telegram.ext import Updater, CommandHandler
TOKEN="token_here"
def start_function(update, context):
update.message.reply_text("Test 12345", parse_mode=telegram.ParseMode.MARKDOWN)
bot = telegram.Bot(token=TOKEN)
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start_function))
updater.start_polling()
Any pointer to docs or examples is welcome. Thank you in advance.
MarkDown
Wrap the number in ``(backtick);
Test: `12345`
HTML
Wrap the number in a <code></code> block;
Test: <code>12345</code>
MarkDown example (first witout ``, second request;):
&text=Test: `12345`&parse_mode=MarkDown

pyTelegramBotAPI disable link preview

Currently writing my first bot using pyTelegramBotAPI. I want to disable link previews on certain messages. How do I do this?
It looks like there is an disable_web_page_preview parameter on the sendMessage method.
tb = telebot.TeleBot(TOKEN)
tb.send_message(123456, "Hi <link>", disable_web_page_preview=True)
Original code;
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None, disable_notification=None):
Try using link_preview/disable_web_page_preview parameter.
client.send_message('chat_name', '[link](example.com)', parse_mode = "Markdown", link_preview=False)
To me this works:
client.send_message(chat_name, msg, link_preview=False)
(Python 3.8, Telethon 1.24)

import string from different file in python

Last week I installed the Telegram application on my Raspberry Pi and set up a script to send me notifications on time (with crontab). But as I have to enter a Token from my Bot and a chat_id of my Telegram Account I want to store them one time in different files so I only have to change it in one file if they ever change. So far my code looks like this:
telepot.py:
import telepot
import values
with open("values.py", "r") as valuesFile:
chat_id, Token = valuesFile.readlines()
bot = telepot.Bot('TOKEN')
bot.sendMessage(chat_id, 'message')
values.py:
chat_id = 'ChatID'
Token = 'TOKEN'
But I haven't figured out how to get the information from my other files. I have looked on the internet but I'm not really good a programming like at all so I hoped somebody could help me with finding the right command to import the two strings from my files and use them as the declaration for chat_id and TOKEN.
Your question is rather unclear. Are you importing the values and tokens from a python file? A text file?. I will guide you through a few examples.
If you want to import the values from another python file (let's call it values.py and let's assume it's in the same directory as the script you sent (telepot.py))
values.py
chat_id = 'YOUR_CHAT_ID'
TOKEN = 'YOUR_TOKEN'
telepot.py
import values
import telepot
bot = telepot.Bot(values.TOKEN)
Now, let's assume the values you need are in a text file, values.txt that looks like:
TOKEN
CHAT_ID
telepot.txt
import telepot
with open("values.txt", "r") as valuesFile:
chatId, Token = valuesFile.readlines()
bot = telepot.Bot(Token)
bot.sendMessage(chatId, "This is a message")

How to send bold text using Telegram Python bot

I am writing a telegram bot in Python. I want to send messages with bold letters. I tried to inclose message inside both * and **, but it does not solve the problem.
Is there a function for mark up or HTML formatting or a way to do it?
You should use:
bot.send_message(chat_id=chat_id, text="*bold* Example message",
parse_mode=telegram.ParseMode.MARKDOWN)
Or:
bot.send_message(chat_id=chat_id, text='<b>Example message</b>',
parse_mode=telegram.ParseMode.HTML)
More info at:
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#message-formatting-bold-italic-code-
This is a little bit late. But i hope to be helpful for others:
import telepot
token = 'xxxxxxxxxxxxxxx' # Your telegram token .
receiver_id = yyyyyyyy # Bot id, you can get this by using the next link :
https://api.telegram.org/bot<TOKEN>/getUpdates. Note that you should
replace <TOKEN> with your token.
bot = telepot.Bot(token)
message = "*YOUR MESSAGE YOU WENT TO SEND.*" #Any characters between ** will be
send in bold format.
bot.sendMessage(receiver_id, message , parse_mode= 'Markdown' )

Categories